Noob - 自动化VBA剪切和粘贴,循环?

时间:2013-07-16 18:28:34

标签: excel vba loops

我正在尝试格式化大型报告,并希望使用宏。例如,我可能需要每隔一列插入一个新列,然后对其进行着色并插入一些数据。对于插入新列部分,而不是:

Sub Macro1()
'
' Macro1 Macro
'

'
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("E:E").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    .....repeated 1000 times  :-(
End Sub

我希望有更多的东西:

' Do this for columns A - ZZ
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns(move over 2 columns).Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'Run until Columns(":") > "ZZ:ZZ"
End Sub

我熟悉其他语言的OOP,希望有人可以解释这些琐碎的任务!

1 个答案:

答案 0 :(得分:1)

Dim rng As Range, x As Long

Set rng = ActiveSheet.Range("A1:J1")
For x = rng.Cells.Count To 1 Step -1
    rng(x).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Next x