2013-09-28 58 views
0
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'不会拾取任何值。当我执行没有任何断点的代码时,也会发生同样的情况。你能帮忙吗?......在vba中切换断点

回答

1

g1val = Cells(33,i).Value应该是For语句后的第一行。

0

我能想到的3个原因,为什么你的代码可能无法工作

  1. 你是不是做数字比较
  2. 你的细胞,对象可能是指ActiveSheet这可能不是表,你想。完全限定您的细胞对象。
  3. 你的行或列中的值不正确

而且我注意到一两件事。您首先不需要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