VBA函数运行一次时工作,但连续尝试失败



我有一个VBA函数,如下代码所示:

Function stageValueVariance(stage As String, valCol As Long)
On Error Resume Next
For i = 2 To offlineHeight
If Application.VLookup(offline.ListColumns(1).Range(i).value, bce.DataBodyRange, valCol, 0) <> offline.ListColumns(valCol).Range(i).value Then
If oldOut.ListRows.Count <> 0 Then
foundID = Application.WorksheetFunction.Match(offline.ListColumns(1).Range(i), oldOut.ListColumns(1).DataBodyRange, 0)
If foundID <> 0 Then
oldOutPresent = True
End If
ElseIf valComp.ListRows.Count <> 0 Then
foundID = Application.WorksheetFunction.Match(offline.ListColumns(1).Range(i), valComp.ListColumns(1).DataBodyRange, 0)
If foundID <> 0 Then
valCompPresent = True
End If
End If
If oldOutPresent = False And valCompPresent = False Then
With stageValComp.ListRows.Add
.Range(1) = offline.ListColumns(1).Range(i)
.Range(2) = offline.ListColumns(2).Range(i)
.Range(3) = stage
.Range(4) = offline.ListColumns(7).Range(i)
.Range(5) = Application.VLookup(offline.ListColumns(1).Range(i).value, bce.DataBodyRange, valCol, 0)
.Range(6).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="Yes, No"
End With
End If
foundID = 0
oldOutPresent = True
valCompPresent = True
End If
Next i
End Function

我用以下方法在宏中调用它:

'find stage value variances and populate data
Call stageValueVariance("Design", 7)
Call stageValueVariance("Guides", 9)
Call stageValueVariance("Lintels", 11)
Call stageValueVariance("Install", 13)

宏在两个表中搜索差异,valCol变量指定要查找的列。如果在offlinebce(两个表(之间发现差异,则会在第三个表stageValComp中添加一行。

该函数还检查此行的ID是否已存在于另外两个表oldOutvalComp中。所有变量都在公共级别声明,并在调用此函数的宏中设置。

我遇到的问题是,当我第一次打开工作簿并运行宏时,代码会发现差异并将详细信息添加到stageValComp中。对于每一次连续运行代码的尝试,都找不到差异。但是,如果我使用F8逐步执行代码,代码将正确运行并找到差异。

我以前从未遇到过这种类型的错误,所以我有点困惑!任何帮助都将不胜感激!

事实证明,这是因为我在If语句之后将oldOutPresentvalCompPresent设置为True,而我本应将其设置回False。正确的代码(现在正在工作!(是:
Function stageValueVariance(stage As String, valCol As Long)
On Error Resume Next
For i = 2 To offlineHeight
If Application.VLookup(offline.ListColumns(1).Range(i).value, bce.DataBodyRange, valCol, 0) <> offline.ListColumns(valCol).Range(i).value Then
If oldOut.ListRows.Count <> 0 Then
foundID = Application.Match(offline.ListColumns(1).Range(i), oldOut.ListColumns(1).DataBodyRange, 0)
If foundID <> 0 Then
oldOutPresent = True
End If
ElseIf valComp.ListRows.Count <> 0 Then
foundID = Application.Match(offline.ListColumns(1).Range(i), valComp.ListColumns(1).DataBodyRange, 0)
If foundID <> 0 Then
valCompPresent = True
End If
End If
If oldOutPresent = False And valCompPresent = False Then
With stageValComp.ListRows.Add
.Range(1) = offline.ListColumns(1).Range(i)
.Range(2) = offline.ListColumns(2).Range(i)
.Range(3) = stage
.Range(4) = offline.ListColumns(valCol).Range(i)
.Range(5) = Application.VLookup(offline.ListColumns(1).Range(i).value, bce.DataBodyRange, valCol, 0)
.Range(6).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="Yes, No"
End With
End If
foundID = 0
oldOutPresent = False
valCompPresent = False
End If
Next i
End Function

相关内容

最新更新