对于i循环和每个循环只是没有跳跃,因为我想要VBA

时间:2017-09-22 09:00:03

标签: vba loops

我创建了这个VBA代码,但它没有像我希望的那样循环遍历每个工作表。我只是在活动工作表上执行任务。 有人可以帮助我吗?

Public Sub MvColumns()

Dim i As Long

For i = 2 To ThisWorkbook.Worksheets.Count
        Columns("D:D").Select
        Selection.Insert Shift:=xlToRight
        Columns("F:F").Select
        Selection.Cut Destination:=Columns("D:D")
        Columns("F:F").Select
        Selection.Delete Shift:=xlToLeft
        Columns("G:G").Select
        Selection.Cut Destination:=Columns("J:J")
        Columns("H:H").Select
        Selection.Cut Destination:=Columns("G:G")
        Columns("H:J").Select
        Selection.Delete Shift:=xlToLeft
        Columns("A:A").Select
        Selection.Delete Shift:=xlToLeft
        Columns("A:F").Select
        Columns("A:F").EntireColumn.AutoFit
        Columns("A:A").Select
        Selection.ColumnWidth = 18.43
        Range("C4").Select
        Range("A2").ClearContents
Next


End Sub
code here

3 个答案:

答案 0 :(得分:0)

您的代码只是在活动工作表上执行任务,因为默认情况下

Columns("D:D").Select 

并将其他行应用于活动工作表。

要将任务应用于所选工作表,您必须使用"使用"像这样

With ThisWorkbook.Sheets("MyWantedSheet")
    .Columns("D:D").Select 'the dot link your code with "MyWantedSheet"
    Selection.Insert Shift:=xlToRight
     'your entire code
End With

在您的情况下,您正在使用循环,以便您可以这样做:

For each sheet in ThisWorkbook.Worksheets ' loop over all sheets 
    With sheet 'select the sheet and apply task on it
         .Columns("D:D").Select
         Selection.Insert Shift:=xlToRight
         'your entire code
    End With
Next

答案 1 :(得分:0)

您需要观看此系列视频:Excel VBA Introduction - YouTube。这是最相关的:Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset)

Public Sub MvColumns()
    Dim i As Long
    For i = 2 To ThisWorkbook.Worksheets.Count
        With ThisWorkbook.Worksheets(i)
            .Columns("D").Insert Shift:=xlToRight
            .Columns("F").Cut Destination:=Columns("D:D")
            .Columns("F").Delete Shift:=xlToLeft
            .Columns("G").Cut Destination:=Columns("J:J")
            .Columns("H").Cut Destination:=Columns("G:G")
            .Columns("H:J").Delete Shift:=xlToLeft
            .Columns("A").Delete Shift:=xlToLeft
            .Columns("A:F").EntireColumn.AutoFit
            .Columns("A").ColumnWidth = 18.43
            .Range("A2").ClearContents
        End With
    Next
End Sub

答案 2 :(得分:0)

Dim For 行替换为:

Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    ws.Activate

这应该可以实现您所要求的 - 但如果您愿意,您还可以清理代码中不必要的部分(并稍微加快速度),如下所示:

Public Sub MvColumns()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Columns("D").Insert Shift:=xlToRight
        ws.Columns("F").Cut Destination:=Columns("D")
        ws.Columns("F").Delete Shift:=xlToLeft
        ws.Columns("G").Cut Destination:=Columns("J")
        ws.Columns("H").Cut Destination:=Columns("G")
        ws.Columns("H:J").Delete Shift:=xlToLeft
        ws.Columns("A").Delete Shift:=xlToLeft
        ws.Columns("A:F").EntireColumn.AutoFit
        ws.Columns("A").ColumnWidth = 18.43
        ws.Range("A2").ClearContents
    Next
End Sub