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

Excel VBA函数在运行一次时工作,但在连续尝试时失败,excel,vba,Excel,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) <> offlin

我有一个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
宏正在两个表中搜索差异,
valCol
变量指定要查找的列。如果在
离线
bce
(两个表)之间发现差异,则将向第三个表添加一行-
stageValComp

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

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


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

结果是因为我在If语句之后将
oldOutPresent
valCompPresent
设置为
True
,而我本应该将其设置回
False
。正确的代码(现在正在工作!)是:

函数stageValueVariance(stage为字符串,valCol为长)
出错时继续下一步
对于i=2至离线高度
如果Application.VLookup(脱机.ListColumns(1).Range(i).value,bce.DataBodyRange,valCol,0)脱机.ListColumns(valCol).Range(i).value,则
如果oldOut.ListRows.Count为0,则
foundID=Application.Match(脱机.ListColumns(1).Range(i),oldot.ListColumns(1).DataBodyRange,0)
如果foundID为0,则
oldOutPresent=True
如果结束
ElseIf valComp.ListRows.Count 0然后
foundID=Application.Match(脱机.ListColumns(1).Range(i),valComp.ListColumns(1).DataBodyRange,0)
如果foundID为0,则
valCompPresent=True
如果结束
如果结束
如果oldOutPresent=False且valCompPresent=False,则
使用stageValComp.ListRows.Add
.Range(1)=脱机.ListColumns(1).Range(i)
.Range(2)=脱机.ListColumns(2).Range(i)
.范围(3)=阶段
.Range(4)=脱机.ListColumns(valCol).Range(i)
.Range(5)=Application.VLookup(脱机.ListColumns(1).Range(i).value,bce.DataBodyRange,valCol,0)
.Range(6).Validation.Add类型:=xlValidateList,AlertStyle:=xlValidAlertStop,公式1:=“是,否”
以
如果结束
foundID=0
oldOutPresent=False
valCompPresent=False
如果结束
接下来我
端函数

摆脱错误时的
,下一步继续
。。。现在有错误吗?如果有,是什么消息/什么行?对。。。但就目前而言,它隐藏了所有错误,而不仅仅是
工作表function.Match
中的潜在错误。也就是说,我将使用后期绑定的
Application.Match
,就像您使用
Application.Vlookup
。因为如果没有找到匹配项,
Application.Match
将返回错误值,因此,您必须测试结果是否是一个数字。
On Error Resume Next
会损害您的整个功能-任何错误都会被不必要地默默忽略。我不返回0,它返回一个错误值。如注释中所述,不要像这样使用
On Error Resume Next
。这是一个非常糟糕的主意……是的,有几次@BigBen注意到,你对另一种方法有什么建议吗?真正的错误处理不是我熟悉的术语。我将具体地更改我的代码为“true”作为一个毯子<代码>在错误恢复下的下一个< /代码>根本不能考虑错误处理…但也许开始吧。
'find stage value variances and populate data
    Call stageValueVariance("Design", 7)
    Call stageValueVariance("Guides", 9)
    Call stageValueVariance("Lintels", 11)
    Call stageValueVariance("Install", 13)
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