显示循环产生的最大值

时间:2014-03-14 12:41:21

标签: vba excel-vba excel

我编写了一个循环(见下文),它产生了sr:10,0,7,5,3和7的低位值。我现在想要修改代码,以便选择最大的sr值 - 在这种情况下10.我搜索了intrernet,但没有太多运气。你能帮忙吗?

谢谢,

奥利

代码:

Sub Macro4()

Dim i As Integer
Dim x As Integer
Dim sr As Long

For i = 2 To 7

x = i - 2

If Cells(4, i).Value <> 0 Then

sr = Range(Cells(3, i), Range("B3").Offset(0, x).End(xlDown)).Rows.Count

Else

sr = 0

'Debug.Print (sr)' - produces values: 10, 0, 7, 5, 3, and 7 

End If

End Sub

1 个答案:

答案 0 :(得分:0)

怎么样:

Sub Macro4()
    Dim i As Integer
    Dim x As Integer
    Dim sr As Long
    Dim Old As Long
    Old = -99999
    For i = 2 To 7
        x = i - 2
        If Cells(4, i).Value <> 0 Then
            sr = Range(Cells(3, i), Range("B3").Offset(0, x).End(xlDown)).Rows.Count
        Else
            sr = 0
        'Debug.Print (sr)' - produces values: 10, 0, 7, 5, 3, and 7
        End If
        If sr > Old Then
            Old = sr
        End If
    Next i
    MsgBox Old
End Sub