Sub Test()
Dim g1val As Integer
g1val = 0
For i = 3 To 27
If g1val >= Cells(33, i).Value Then
g1val = g1val
ElseIf g1val < Cells(33, i).Value Then
g1val = Cells(33, i).Value
End If
Next i
End Sub
这里当我在第3行放置切换断点并由F8执行时,更快的执行'g1val'不会获取任何值。当我执行没有任何断点的代码时也会发生同样的情况。你可以帮忙吗?? ....
答案 0 :(得分:1)
g1val = Cells(33,i).Value
应该是For语句之后的第一行。
答案 1 :(得分:1)
我可以想到代码可能无法正常工作的3个原因
ActiveSheet
,它可能不是您想要的工作表。完全限定您的单元格对象。我还注意到了一件事。在将IF
的值设置为g1val
时,您不需要g1val
。您可以将代码编写为
Sub Test()
Dim g1val As Long, i As Long
Dim ws As Worksheet
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
For i = 3 To 27
If g1val < Val(Trim(.Cells(33, i).Value)) Then _
g1val = Val(Trim(.Cells(33, i).Value))
Next i
End With
Debug.Print g1val
End Sub