用VBA对excel中的数据进行操作的问题

2026年09月02日 20:47
有2个网友回答
网友(1):

第一个按钮


Sub 最大值减一()

For rs = 2 To 21

    For col = 2 To 7

    b = Application.Max(Sheets("数据2").Range("b2:g21"))

     If Sheets("数据2").Cells(rs, col) = b Then

        Sheets("数据2").Cells(rs, col) = b - 1

     End If

    Next col

Next rs

End Sub


第二个按钮


Sub 所有值减一()

For rs = 2 To 21

    For col = 2 To 7

    a = Cells(rs, col)

     Sheets("数据2").Cells(rs, col) = a - 1

    Next col

Next rs

End Sub


以上的代码都是我经过测试的。

网友(2):

'按钮1
Private Sub CommandButton1_Click()
    dim rng as range,amax as double
    set rng=worksheets("数据2").range(B2:G21)
    amax = Application.WorksheetFunction.Max(rng)
    for each cl in rng
        if amax=cl.value then
            cl=cl-1
            exit for
        end if
    next
end sub
'按钮2
Private Sub CommandButton2_Click()
    dim rng as range
    set rng=worksheets("数据2").range(B2:G21)
    arr=rng
    for i=1 to ubound(arr,1) 
        for j=1 to ubound(arr,2)
            if isnumeric(arr(i,j)) then
                arr(i,j)=arr(i,j)-1
            end if
        next
    next
    rng=arr
end sub