特定代码行在没有指示的情况下跳转到 GoTo 部分

Specific line of code is jumping to a GoTo section without being instructed to

提问人:Benedict Landauer 提问时间:11/15/2023 更新时间:11/15/2023 访问量:47

问:

我正在编写一个 VBA,用于标识活动工作簿中潜在客户工作表上已用范围的最后一行,并循环浏览其他工作表的指定数组以扩展已用范围中的所有公式。然后,它会编译一个消息框,该消息框传达工作表名称、最后一行(应与所有工作表匹配)以及该工作表最后一行的日期列中的值(也应为所有工作表相同)。消息框有助于突出显示是否有任何问题。

当工作表确实存在时,就会出现问题,并且代码继续分配最后一行变量。有一行特定的代码,在执行后,它会导致 VBA 跳转到命名部分 (SkipSheet),尽管没有给出这方面的说明。

这是我代码的相关部分:

    ' Loop through target sheets
    For Each TS In TSs
        ' Try to set the target sheet
        On Error GoTo SkipSheet
        Set ws = AW.Sheets(TS)

                ' Find the column number where 'Date' appears in row 3
                Dim dateColumn As Long
                dateColumn = Application.Match("Date", ws.Rows(3), 0)
                
                ' Check if 'Date' is found in row 3
                If IsError(dateColumn) Then
                    lastRowTS = "Date column not found"
                    Else
                ' Get the last row of the target sheet in the determined column
                    lastRowTS = ws.Cells(ws.Rows.Count, dateColumn).End(xlUp).Row
                    lastRowVL = ws.Cells(lastRowTS, dateColumn).Value
                End If
            
                ' Extend formulas to the last row of the lead sheet
                ws.Rows(lastRowTS & ":" & lastRowMD).FillDown
                RTN = RTN & TS & " = " & lastRowTS & " - " & lastRowVL & vbCrLf
            
NextSheet:
    Next TS
    GoTo EndOfSheets
    
SkipSheet:
    Set ws = Nothing
    RTN = RTN & TS & " = No Sheet" & vbCrLf
    Resume NextSheet
   
EndOfSheets:
    ' Display the message box
    MsgBox RTN

意外跳转发生在以下行之后: ws。行(lastRowTS &“:” & lastRowMD)。填充

这导致 RTN 变量未设置,从而搞砸了最终消息框,从而产生误报。

我尝试添加一个额外的 IF 条件来包含“ws.行(...”line和 “RTN =” 行,如下所示,但问题仍然存在,在 ws 之后跳转到 SkipSheet。行:

        If Not ws Is Nothing Then
                ' Find the column number where 'Date' appears in row 3
                Dim dateColumn As Long
                dateColumn = Application.Match("Date", ws.Rows(3), 0)
                
                ' Check if 'Date' is found in row 3
                If IsError(dateColumn) Then
                    lastRowTS = "Date column not found"
                    Else
                ' Get the last row of the target sheet in the determined column
                    lastRowTS = ws.Cells(ws.Rows.Count, dateColumn).End(xlUp).Row
                    lastRowVL = ws.Cells(lastRowTS, dateColumn).Value
                End If
            
                ' Extend formulas to the last row of the lead sheet
                ws.Rows(lastRowTS & ":" & lastRowMD).FillDown
                RTN = RTN & TS & " = " & lastRowTS & " - " & lastRowVL & vbCrLf
            Else
                GoTo SkipSheet
            End If

任何帮助将不胜感激,因为我完全不知道出了什么问题!如果我错过了任何必需的信息,请告诉我。

Excel VBA 错误处理

评论

1赞 SJR 11/15/2023
如果是字符串,则会出错。在哪里分配值?lastRowTSlastRowMD
3赞 SJR 11/15/2023
我不认为通常建议使用大量的 Gotos。

答:

3赞 Ike 11/15/2023 #1

我会避免这种逻辑。GOTO

请改用如下所示的 -方法:try

Public Function tryGetWS(strName As String, wb As Workbook, _ 
                         ByRef ws As Worksheet) As Boolean
On Error Resume Next
    Set ws = wb.Worksheets(strName)
    If Err = 0 Then tryGetWS = True
On Error GoTo 0
End Function

如果工作表存在,则函数将返回工作表本身。true

然后,您可以像这样使用它:

Sub test()
Dim TSs As Variant: TSs = Array("a", "b", "Tabelle1")

Dim TS As Variant, ws As Worksheet
For Each TS In TSs
    If tryGetWS(CStr(TS), ThisWorkbook, ws) = True Then
        Debug.Print ws.Name
    Else
        Debug.Print TS & " not found"
    End If
Next

End Sub

这种方法的另一个优点是:很清楚函数的作用 - 无需读取代码本身:-)