excel 特定的代码行跳转到一个后藤部分,而没有指示

xoefb8l8  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(106)

我正在编写一个函数,它标识活动工作簿中的一个引导表上所用范围的最后一行,并在指定的其他工作表数组中循环,以扩展所用范围中的所有公式。(这应该与所有工作表匹配,并且该工作表中最后一行的日期列中的值(也应该与所有工作表相同)。
当工作表实际上存在时,问题就出现了,代码继续分配最后一行变量。有一行特定的代码,在执行后,会导致工作表跳转到一个命名的部分(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. jump(lastRowTS &“:“& lastRowMD).FillDown
这会导致RTN变量没有被设置,这会导致最终的消息框出现假阴性。
我尝试添加一个额外的IF条件来包含“ws. config(...”行和“RTN =”行,如下所示,但问题仍然存在,跳转到ws. config之后的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


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

mum43rcc

mum43rcc1#

我会避免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


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

相关问题