基本循环(excel不能一致地识别代码)

我正在使用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但是我已经尝试了两个,我不知道有什么问题! 就像我说的,有时循环工作正常,有时它保持循环,直到它说:“溢出”

我将不胜感激您的帮助! 我一直在挣扎着这些日子!

你的溢出很可能是由于任何Integertypes的ZJvariables的声明而产生的,它被限制在最大值32767

所以你可以试着把它们声明为Longtypes

除此之外,您可以使用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