我已经编写了嵌套的For循环,即使满足了条件,它也不会执行For循环的代码。我试着注释掉最外层的For循环,但内部循环也不起作用。我在Excel2007工作
Sub CalcAll()
Dim a As Integer
a = 10
Dim b As Integer
b = 10
For a = 10 To a = (Range("B" & Rows.Count).End(xlUp).Row) Step 1
For b = 10 To b = (Worksheets("DistanceLookupTable").Cells(2, Sheet1.Columns.Count).End(xlToLeft).Column) Step 1
If IsEmpty(Cells(a, i).Value) Then
Exit Sub
Else
'Lots of code reading values from the worksheet and printing
'calculated values to the worksheet
End If
Next b
Next a
End Sub
感谢您的帮助
您的For循环应该写成:
For a = 10 To XXX
而不是:
For a = 10 To a = XXX
试试这个:
Dim a As Integer
'a = 10 'Unnecessary to assign value here, as you assign the starting value in the For loop
Dim b As Integer
'b = 10 'Again, this line not necessary
For a = 10 To Range("B" & Rows.Count).End(xlUp).Row Step 1
For b = 10 To Worksheets("DistanceLookupTable").Cells(2, Sheet1.Columns.Count).End(xlToLeft).Column Step 1
If IsEmpty(Cells(a, i).Value) Then '<- do you mean 'b' instead of 'i'? I don't see 'i' assigned anywhere...
Exit Sub
Else
'Lots of code reading values from the worksheet and printing
'calculated values to the worksheet
End If
Next b
Next a
在一个无关的注意事项上,您可能会考虑在第一个for循环中完全限定您的范围(Worksheets("worksheetName").Range("B" & Rows.Count)...
而不仅仅是Range("B" & Rows.Count)...
)。现在,它将使用当前活动工作表的范围。所以,除非这是你的意图,否则最好明确一点。