尝试循环多个工作表VBA时出错

时间:2016-10-07 14:38:24

标签: excel vba excel-vba foreach

我已编写此代码以循环执行多个工作表并执行某项任务,但出于某种原因,我被告知Subscript超出了此行的范围:

Sheets(Worksheetss(indexVal)).Range("B4:S25").Select

完整的代码可以在这里看到:

Sub kopierforsatan()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False

Dim Worksheet As Variant
Dim Worksheetss As Variant 
Dim outputs As Variant

Worksheetss = Array("6_år_lav", "6_år_middel", "6_år_høj", "10_år_høj")


Dim indexVal As Integer
indexVal = 0

For Each Worksheet In Worksheetss


Sheets(Worksheetss(indexVal)).Range("B4:S25").Select
Application.CutCopyMode = False
Selection.Copy

Sheets(Worksheetss(indexVal)).Range("V4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False
Selection.Copy

Sheets(Worksheetss(indexVal)).Range("V30").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=True
Sheets(Worksheetss(indexVal)).Range("B54").Select
Sheets(Worksheetss(indexVal)).Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy

Sheets(Worksheetss(indexVal)).Range("V50").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=True
Application.CutCopyMode = False
Selection.Copy

Sheets(Worksheetss(indexVal)).Range("Y30").Select
ActiveSheet.Paste

indexVal = indexVal + 1
Next Worksheet


End Sub

最让我感到困扰的是,我之前使用过这种语法,就每个循环的构造方式而言。

我希望有人能看到我犯了错误的地方。

4 个答案:

答案 0 :(得分:2)

请参阅@ comintern的评论It's a horrible idea to use them for variable names, but VBA determines the context from scope and usage. If you have Dim Worksheets As Variant it just means that you have to qualify ThisWorkbook.Worksheets. Again, not the best idea.,了解为什么不应将worksheet用作变量。

另请尝试避免使用Select。

第三,当多次使用for循环时,不需要计数器。只需参考您创建的变量即可。

Sub kopierforsatan()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False

Dim ws As Variant
Dim Worksheetss As Variant
Dim outputs As Variant

Worksheetss = Array("6_år_lav", "6_år_middel", "6_år_høj", "10_år_høj")


For Each ws In Worksheetss
    With Sheets(ws)
        .Range("B4:S25").Copy
        .Range("V4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        .Range("V30").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
        .Range(.Range("B54"), .Range("B54").End(xlDown)).Copy .Range("V50")
        .Range(.Range("B54"), .Range("B54").End(xlDown)).Copy .Range("Y30")
    End With
Next ws

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayStatusBar = True

End Sub

答案 1 :(得分:0)

您指的是Worksheetss,它应该是Worksheets

答案 2 :(得分:0)

您可以遍历工作簿中的所有工作表,然后检查当前工作表的名称是否等于数组中的名称。

(我更喜欢使用名为Sht的变量而不是接近`Worksheet1的变量。)

Dim Sht     As Worksheet       

For Each Sht In ThisWorkbook.Sheets
    With Sht
        Select Case .Name
            Case "6_år_lav", "6_år_middel", "6_år_høj", "10_år_høj"
                Application.CutCopyMode = False
                .Range("B4:S25").Copy
                .Range("V4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                    :=False, Transpose:=False

                ' etc... continue the rest of your copy > Paste            

        End Select
    End With     

Next Sht

答案 3 :(得分:0)

非常感谢你所有的贡献和答案,然后那么快!

我设法使用@Scott Craners实现了一个解决方案,稍作修改。 修改只是必要的,因为我需要从B54转换创建矢量。

我会记住你们关于变量名称的约定讨论了什么,并看看有关使用select和activate的链接。

非常感谢您的时间。

周末愉快。