我有一张包含用户定义的输入和输出值的工作表。代码将针对大多数组合输入和输出值运行,但对于某些代码,它将返回我要求的数据,然后进入无限循环。
我在想添加一个变量和几行代码,其中变量计算循环遍历整行的次数。一旦变量的值等于运行次数的2倍,它就会退出子。
是否有正确的方法来编写此代码,是否可能?
提前谢谢
Sub SuperNova()
Dim Startval As String
Dim Endval As String
Dim LastRow As Long 'SAVE THIS - DON'T TOUCH
Dim Rng As range 'SAVE THIS - DON'T TOUCH
Dim Output As Long 'SAVE THIS - DON'T TOUCH
Dim NewStart As String
Dim Val As String
Dim Valnew As String
LastRow = Worksheets(3).range("B" & Rows.Count).End(xlUp).Row
Output = 2
Startval = Worksheets(3).Cells(7, "AC")
NewStart = Startval
Line1:
For X = 7 To LastRow
Val = Worksheets(3).Cells(X, 2) 'if i set the variables equal to a string, do i need the GetSingleFromString function?
Endval = Worksheets(3).Cells(7, "AD")
If Val = Endval And Valnew = Endval Then
Exit Sub
ElseIf StrComp(Val, NewStart) = 0 Then
Worksheets(4).Cells(Output, 1).Value = _
Worksheets(3).Cells(X, 1).Value
Output = Output + 1
Worksheets(4).Cells(Output, 1).Value = _
Worksheets(3).Cells(X, 1).Value
Output = Output + 1
Valnew = Worksheets(3).Cells(X, 3)
NewStart = Valnew
End If
Next X
If Valnew = Endval Then
Exit Sub
Else
GoTo Line1
End If
End Sub
这是我的Excel工作表:在单元格中输入B列中的值" AC7"和C列中的值到单元格" AD7"然后运行代码,并应粘贴Sheet4。组合如" 1到4"将工作。但如果我给AC7一个值为" 10"和AD7的值为" 6"它返回我需要的A列中的值,但随后会遇到无限循环并崩溃。