如何引用工作表来遍历不同的工作表并在每个工作表上执行相同的功能?

时间:2017-05-03 03:32:29

标签: excel vba excel-vba

我是VBA的新手,正在努力学习它。我已经编写了这个函数来尝试遍历同一工作簿中的一些工作表,并添加一列来更新标记为“ww'基于左侧的单元格。

我遇到了For循环的问题,它应该循环定义的sh1到sh3。如果使用工作表,如何声明工作表循环。(" sh"& i)不适用于For循环?

Sheets("sh" & i).Activate部分是我收到调试错误的地方。 "运行时错误:9 下标超出范围"

我在很大程度上不确定自己在做什么和做多少实验。从某方面来说,是否有任何优秀的在线学习课程可以用来增加我对VBA的知识?

Function updateWWColumn()

    Set sh1 = ActiveWorkbook.Sheets("Data_WE")
    Set sh2 = ActiveWorkbook.Sheets("Data_E")
    Set sh3 = ActiveWorkbook.Sheets("Data_PC")

    'placeholder to contain the week number in integer form from the cell on the right.
    Dim placeholder As Integer
    Dim i As Integer    

    For i = 1 To 3
        Sheets("sh" & i).Activate ' Run time error: 9 subscript out of range
        ActiveSheet.Columns("U:U").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Worksheets("sh" & i).Columns("P:P").Delete Shift:=xlToLeft
        placeholder = Right(Worksheets("sh" & i).Range("S83"), 2)
        Worksheets("sh" & i).Range("T83").Value = "WW" & placeholder + 1
    Next i

End Function

3 个答案:

答案 0 :(得分:3)

您可以使用工作表数组:

Public sh(1 To 3) As Worksheet

Sub updateWWColumn()  ' Only use Function when you are returning a value

    Set sh(1) = ActiveWorkbook.Worksheets("Data_WE")
    Set sh(2) = ActiveWorkbook.Worksheets("Data_E")
    Set sh(3) = ActiveWorkbook.Worksheets("Data_PC")
    'placeholder to contain the week number in integer form from the cell on the right.
    Dim placeholder As Integer ' Note: Integer, not Interger
    Dim i As Integer

    For i = 1 To 3
        With sh(i)
            .Columns("U:U"). _
                Insert Shift:=xlToRight, _
                CopyOrigin:=xlFormatFromLeftOrAbove

            .Columns("P:P"). _
                Delete Shift:=xlToLeft

            placeholder = Right(.Range("S83"), 2)
            .Range("T83").Value = "WW" & placeholder + 1
        End With
    Next i

End Sub

或者,您可以使用其名称遍历每个工作表:

Sub updateWWColumn()  ' Only use Function when you are returning a value
    Dim ws As Worksheet

    'placeholder to contain the week number in integer form from the cell on the right.
    Dim placeholder As Integer ' Note: Integer, not Interger
    Dim i As Integer

    For Each ws In ActiveWorkbook.Worksheets(Array("Data_WE", "Data_E", "Data_PC"))
        With ws
            .Columns("U:U"). _
                Insert Shift:=xlToRight, _
                CopyOrigin:=xlFormatFromLeftOrAbove

            .Columns("P:P"). _
                Delete Shift:=xlToLeft

            placeholder = Right(.Range("S83"), 2)
            .Range("T83").Value = "WW" & placeholder + 1
        End With
    Next

End Sub

答案 1 :(得分:3)

不要使用三个变量来存储工作表引用 - 使用存储特定工作表的三个项目的数组。

所以我在这里所做的就是将sh1,sh2和sh3改为一个名为sh的数组。然后,只要您使用WorkSheets("sh"&i),就可以使用sh(i)

Function updateWWColumn()
    Dim sh(3) As Worksheet
    Set sh(1) = ActiveWorkbook.Sheets("Data_WE")
    Set sh(2) = ActiveWorkbook.Sheets("Data_E")
    Set sh(3) = ActiveWorkbook.Sheets("Data_PC")
    'placeholder to contain the week number in integer form from the cell on the right.
    Dim placeholder As Interger
    Dim i As Integer

    For i = 1 To 3
        sh(i).Activate
        ActiveSheet.Columns("U:U"). _
        Insert Shift:=xlToRight, _
        CopyOrigin:=xlFormatFromLeftOrAbove

        sh(i).Columns("P:P"). _
        Delete Shift:=xlToLeft

        placeholder = Right(sh(i).Range("S83"), 2)
        sh(i).Range("T83").Value = "WW" & placeholder + 1
    Next i
End Function

答案 2 :(得分:0)

其他发布的答案都很好。或者,您可以遍历工作簿中的每个工作表:

Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
    sht.Activate
    ' etc...
Next sht

或者,如果您只想影响以名称Data_开头的工作表,则可以嵌入if语句:

Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
    If Left(sht.Name, 5) = "Data_" Then
        sht.Activate
        ' etc...
    End If
Next sht