【问题标题】:Runtime Error 9 while selecting the Sheet in ActiveWorkbook在 ActiveWorkbook 中选择工作表时出现运行时错误 9
【发布时间】:2022-08-13 23:27:36
【问题描述】:

在 Sheets(2).Select 行运行以下子例程时出现运行时错误 9。我在即时窗口中检查了 Activeworkbook.Name 我得到了正确的工作簿名称。但不确定为什么会引发下标超出范围错误。 ThisWorkbook 只有 sheet1,所以我猜它是指 ThisWorkbook 而不是 ActiveWorkbook。如何纠正它。我还尝试了其他代码行,但没有帮助。 ActiveSheet.Next.Select 工作表(1).Next.Select 如果所有工作簿中的 A5 值为空白,则该子例程将清除格式。

Sub REReplace()
Dim Folder As String, FileName As String
Dim tWB, w As Workbook

Application.ScreenUpdating = False

Set tWB = ThisWorkbook
Folder = \"C:\\New\\test\"
FileName = Dir(Folder & \"\\*.xlsx\")
Do
    Workbooks.Open Folder & \"\\\" & FileName
    FileName = Dir
Loop Until FileName = \"\"

    For Each w In Workbooks
    If Not w Is ThisWorkbook Then
        w.Activate
        Sheets(2).Select
        
        If Sheets(2).Range(\"A5\").Value = \"\" Then
        Sheets(2).Range(\"A5\").Select
        Sheets(2).Range(Selection, Selection.End(xlToRight)).Select
        Selection.ClearFormats
        Sheets(2).Range(\"A3\").Select
        End If
        
        w.Close SaveChanges:=True
    End If
Next w
Application.ScreenUpdating = True
End Sub

下面的代码将文档编号的最后一个值从 1 替换为 2,例如 BCR-98946210371-001 到 BCR-98946210371-002,并从 sheet1 中的单元格 D1:D8 中删除格式。现在我在我的问题中发布的 sheet2 中有额外的要求。如果 A5 为空白,我需要清除 A5 行中的格式。

**********Old code in sheet1**********

     Sub REReplace()
     Dim Folder As String, FileName As String
     Dim tWB, w As Workbook
     Dim n As String
     Dim j As String, Ex As String, Con, l As String
     Dim o As Integer, p As Integer, u As Integer

     Application.ScreenUpdating = False
     \'j = \"2\"
     Set tWB = ThisWorkbook
     Folder = \"C:\\New\\test\"
     FileName = Dir(Folder & \"\\*.xlsx\")
     Do
       Workbooks.Open Folder & \"\\\" & FileName
       FileName = Dir
     Loop Until FileName = \"\"

     For Each w In Workbooks
        If Not w Is ThisWorkbook Then
            w.Activate
            Set w = ActiveWorkbook
            Sheets(1).Select  \'In sheet 1 B1 value is changed to ver 2 
            Range(\"A1\").Select
            l = Range(\"B1\").Value
            o = Len(l)
            p = Right(l, 1)
            u = o - p
            Ex = Left(l, u)
            Con = Ex & j
            Ex = Left(l, u)
            Con = Ex & j
            Range(\"B1\").Value = Con
            Range(\"D1:D8\").ClearFormats
        End if
      Next w
***********New code in sheet2 shown below***********

     For Each w In Workbooks
       If Not w Is ThisWorkbook Then
          With w
           .Activate
           If .Sheets.Count >= 2 Then
                .Sheets(2).Select
                If .Sheets(2).Range(\"A5\").Value = \"\" Then
                    .Sheets(2).Range(\"A5\").Select
                    .Sheets(2).Range(Selection, _ 
                     Selection.End(xlToRight)).ClearFormats
                    .Sheets(2).Range(\"A3\").Select
                    .Sheets(1).Select
                End If
                Stop
                .Close SaveChanges:=True
             End If
          End With
       End If
      Next w
      Application.ScreenUpdating = True
      End Sub

    标签: excel vba macos


    【解决方案1】:

    因此,为了帮助您轻松编写代码,我会在错误之前添加 Sheets.Count。另外,if 对我不起作用,所以我添加了Thisworkbook.Name 见下文:

    For Each w In Workbooks
        If Not w.Name = ThisWorkbook.Name Then
            With w
               .Activate
                If .Sheets.Count >= 2 Then
                 'Here you can add more Sheets
                    With Sheets(1)
                        'Here You can Add More Code per Sheet
                        .Activate
                        '...
                    End With
                    With Sheets(2)
                        .Activate
                        If .Range("A5").Value = "" Then
                            .Range("A5").Select
                            .Range(Selection, Selection.End(xlToRight)).ClearFormats
                            .Range("A3").Select
                        End If
                    End With
                    .Close SaveChanges:=True
                End If
            End With
        End If
    Next w
    

    尝试使用With 命令,它有助于阅读代码并使其更快。

    其他的,尽量避免.Select,请阅读这篇精彩的帖子并学习如何管理它。 How to Avoid Select

    【讨论】:

    • 有效。但是,即使代码中存在 .Close SaveChanges:=True,工作簿仍保持打开状态。
    • 我不得不删除停止然后它工作。工作簿已保存并关闭。
    • 如何将这两者合并到一个子程序中。 For Each w In Workbooks If Not w Is ThisWorkbook Then w.Activate Set w = ActiveWorkbook Sheets(1).Select Range("A1").Select n = k Range("B13").Value = n l = Range("B1 ").Value o = Len(l) p = Right(l, 1) u = o - p Ex = Left(l, u) Con = Ex & j Ex = Left(l, u) Con = Ex & j 范围("B1").Value = Con Range("D1:D8").ClearFormats end if Next w
    • 我需要将上述评论中的子例程与我发布的子例程合并。
    • 在代码中添加 cmets 以在每个文件和每个工作表中放置更多代码的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多