我正在使用excel中的vba进行销售仪表板,我在使用此循环时遇到了问题。我多次使用相同的循环,有时它可以工作,但有时它不会。 Excel保持循环,无法识别停止循环的条件。
Do Until (Worksheets("aerolineas").Range("xaa3").Offset(Z, 0).Text = cmbAirlines.Text)
Z = Z + 1
Loop
循环有时不起作用的另一个例子
Do
J = J + 1
Loop Until (ActiveSheet.Cells(13, 1).Offset(J, 0).Value = cmbAgents)
我认为它可能与.Value
和.text
有关,但我已经尝试了两者,我无法弄清楚出了什么问题!就像我说的,有时循环工作正常,有时它会循环,直到它说:“溢出”
感谢您的帮助!我这几天一直在努力奋斗!
答案 0 :(得分:0)
您的溢出很可能来自于Z
类型的J
和Integer
变量的声明,其限制为最大值32767
因此您可以尝试将其声明为Long
类型
除此之外,您可以使用Find(
)方法跳过循环:
您的第一个代码将成为
Dim found As Range
Set found = Worksheets("aerolineas").Range("xaa:xaa").Find(what:=cmbAirlines.Text, LookIn:=xlValues, lookat:=xlWhole)
If Not found Is Nothing Then
'your code to exploit 'found' range
MsgBox "found vale at row: " & found.row
End If
,你的第二个代码将成为:
Dim found As Range
Set found = Range("A13", Cells(Rows.count, 1).End(xlUp)).Find(what:=cmbAgents.Text, LookIn:=xlValues, lookat:=xlWhole)
If Not found Is Nothing Then
'your code to exploit 'found' range
MsgBox "found vale at row: " & found.row
End If