2013-09-28 80 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