“退出”不起作用
在Excel VBA中进行反向循环,查找某个列中最后一个已填充的单元格。 一旦find,它应该退出循环,但退出For不工作,并继续循环回来。 有任何想法吗?
rewind: ' so we 're still "under", rollback to the right line While Not Range("I" & a).Value = getsum a = a - 1 On Error GoTo TryCatch If Not Range("E" & a).Value = "" Then Rows(a).Select currfield = Range("E" & a).Value runningstrsum = runningstrsum - currentstrsum 'we're switching streets ' and since we just lost currentstrsum, we have to reset it to the last one, yay For c = a - 1 To 2 Step -1 If Not Range("E" & c).Value = "" Then 'there it is currentstrsum = Range("E" & c).Value Exit For End If Next c End If Wend If overunder < 0 Then 'go back to overunder< GoTo goodjobunder ElseIf overunder = 0 Then GoTo goodjobeven End If
你只是退出内部循环,代码将会恢复到这个循环之外 – 这仍然在While
循环中,因此重新进入For
循环。
如果你想在列中find最后一个填充的单元格,只需使用如下所示的内容:
Dim lastCell As Excel.Range Set lastCell = Range("E" & Rows.Count).End(xlUp)
不需要循环。
debuggingVBA代码也是一个好时机