使用VBA循环遍历工作表CodeNames数组

时间:2015-06-06 23:23:29

标签: arrays vba excel-vba excel

我试图通过VBA循环遍历一系列特定的工作表,但不断收到错误。我已经在这一个星期待了一个多星期了,终于让我自己在这里注册了。我目前拥有的代码如下:

Option Explicit
Sub Create_NewEvent2()

Dim i As Variant, wName As Variant, x As Variant, ws As Worksheet
wName = Array("Sheet1", "Sheet3", "Sheet5", "Sheet7", "Sheet9", _
"Sheet13", "Sheet17", "Sheet21", "Sheet23", "Sheet27", "Sheet31", _ 
"Sheet35", "Sheet39", "Sheet43", "Sheet47", "Sheet54", _
"Sheet56", "Sheet57", "Sheet58", "Sheet60", "Sheet61", "Sheet62", _ 
"Sheet63", "Sheet64", "Sheet65", "Sheet82", "Sheet83", "Sheet84", _
 "Sheet85", "Sheet90", "Sheet91", "Sheet93", "Sheet94")


For Each ws In ActiveWorkbook.Worksheets
For i = LBound(wName) To UBound(wName)
If ws.CodeName = wName(i) Then
 ws.Visible = xlSheetVisible
  ws.Range("M7:M38").Select
   Selection.Copy
    ws.Range("D7").Select
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
     Application.CutCopyMode = False
      ws.Range("G7:M38,E7:E38,P43:P45").Select
        ws.Range("P43").Activate
         Selection.ClearContents
          ws.Visible = xlSheetVeryHidden
    Call AutoStock
     End If
 Next i
 Next ws
 End Sub

我从最后一段代码中得到的错误消息是"选择范围类的方法失败。"当我调试时,它有" ws.Range(" M7:M38")。选择"突出显示,但我已经在其他部分中使用了这种确切的语法,并且运行得很好。任何人都可以告诉我这里出了什么问题吗?任何帮助将不胜感激..

3 个答案:

答案 0 :(得分:1)

Worksheet.CodeName实际上会返回工作表对象。您可以直接使用它们构建数组。

Sub Create_NewEvent2()

    Dim w As Long, vCODENAMEs As Variant

    vCODENAMEs = Array(Sheet1, Sheet3, Sheet5, Sheet7, Sheet9, Sheet13, _
                       Sheet17, Sheet21, Sheet23, Sheet27, Sheet31, Sheet35, _
                       Sheet39, Sheet43, Sheet47, Sheet54, Sheet56, Sheet57, _
                       Sheet58, Sheet60, Sheet61, Sheet62, Sheet63, Sheet64, _
                       Sheet65, Sheet82, Sheet83, Sheet84, Sheet85, Sheet90, _
                       Sheet91, Sheet93, Sheet94)

    For w = LBound(vCODENAMEs) To UBound(vCODENAMEs)
        With vCODENAMEs(w)
            Debug.Print .Name  'string name of the worksheet
            ' all of your other operations here
            '.Visible = xlSheetVisible  'you do not have to unhide for the operations you've chosen if you reference directly
            .Range("D7:D38") = .Range("M7:M38").Value
            .Range("E7:E38,G7:M38,P43:P45").ClearContents
            .Visible = xlSheetVeryHidden
            Call AutoStock
        End With
    Next w

End Sub

通过直接工作表和单元格引用,我完全避免了所有.Activate.Select废话。有关远离依赖选择和激活以实现目标的更多方法,请参阅How to avoid using Select in Excel VBA macros

答案 1 :(得分:0)

Select不适用于未处于活动状态的工作表。 首先调用ws.Activate,然后调用Select

答案 2 :(得分:0)

请参阅修改后的代码,并进行少量修改和评论。 我建议不要使用ActiveWorkbook,因为不一定是你想要使用的那个是活跃的。此外,无需选择您正在使用的范围(@Jeeped引用的注释和文档)

Sub Create_NewEvent2_New()
Dim wName As Variant, vName As Variant
wName = Array("Sheet1", "Sheet3", "Sheet5", "Sheet7", "Sheet9", _
    "Sheet13", "Sheet17", "Sheet21", "Sheet23", "Sheet27", "Sheet31", _
    "Sheet35", "Sheet39", "Sheet43", "Sheet47", "Sheet54", _
    "Sheet56", "Sheet57", "Sheet58", "Sheet60", "Sheet61", "Sheet62", _
    "Sheet63", "Sheet64", "Sheet65", "Sheet82", "Sheet83", "Sheet84", _
    "Sheet85", "Sheet90", "Sheet91", "Sheet93", "Sheet94")

Dim Wbk As Workbook 'Set workbook variable don't use ActiveWorkbook
Dim ws As Worksheet

    Rem Set Workbook - Change as needed if applied to another workbook.
    Set Wbk = ThisWorkbook

    Rem Loop thru List of Worksheets instead of All Worksheets
    For Each vName In wName

        Set ws = Nothing
        On Error Resume Next
        Set ws = Wbk.Worksheets(vName)
        On Error GoTo 0
        If Not (ws Is Nothing) Then
            With ws
                .Visible = xlSheetVisible
                .Range("M7:M38").Copy
                .Range("D7").PasteSpecial Paste:=xlPasteValues
                Application.CutCopyMode = False
                .Range("G7:M38,E7:E38,P43:P45").ClearContents
                .Visible = xlSheetVeryHidden
            End With
            Call AutoStock

    End If: Next

End Sub