在多个选项卡上运行宏

时间:2014-09-12 21:06:02

标签: loops excel-vba vba excel

我们从承包商处收到的文件总是采用相同的格式,需要重新格式化。文件的标签数量可能不同,但命名法始终相同。之前为活动工作表运行的宏。我添加了循环部分来为所有选项卡执行此操作。我们的目标是在承包商提供的excel文件中提供的每个选项卡上运行宏。但是,它不会在存储数据的当前选项卡上对其进行格式化 - 仅在活动工作表上。我知道我需要用Active Sheet改变一些东西,但我迷失了这个。任何帮助都会很棒。 谢谢, 尼克

Sub WorksheetLoop()

     Dim WS_Count As Integer
     Dim I As Integer

     ' Set WS_Count equal to the number of worksheets in the active
     ' workbook.
     WS_Count = ActiveWorkbook.Worksheets.Count

     ' Begin the loop.
     For I = 1 To WS_Count

Cells.Select
Selection.UnMerge
Rows("1:9").Select
Selection.Delete Shift:=xlUp
Range("A2").Select
ActiveCell.FormulaR1C1 = "=R[-1]C"
Range("B2").Select
ActiveCell.FormulaR1C1 = "=R[-1]C"
Columns("F:F").Select
Selection.Delete Shift:=xlToLeft
Columns("I:I").Select
Selection.Delete Shift:=xlToLeft
Columns("L:L").Select
Selection.Delete Shift:=xlToLeft
Columns("O:O").Select
Selection.Delete Shift:=xlToLeft
Columns("R:R").Select
Selection.Delete Shift:=xlToLeft
Columns("U:U").Select
Selection.Delete Shift:=xlToLeft
Columns("X:X").Select
Selection.Delete Shift:=xlToLeft
Columns("AA:AA").Select
Selection.Delete Shift:=xlToLeft
Columns("AD:AD").Select
Selection.Delete Shift:=xlToLeft
Columns("AG:AG").Select
Selection.Delete Shift:=xlToLeft
Columns("AJ:AJ").Select
Selection.Delete Shift:=xlToLeft
Columns("AM:AM").Select
Selection.Delete Shift:=xlToLeft
Columns("AP:AP").Select
Selection.Delete Shift:=xlToLeft
Columns("AS:AS").Select
Selection.Delete Shift:=xlToLeft
Columns("AV:AV").Select
Selection.Delete Shift:=xlToLeft
Columns("AY:AY").Select
Selection.Delete Shift:=xlToLeft
Columns("BB:BB").Select
Selection.Delete Shift:=xlToLeft
Columns("BE:BE").Select
Selection.Delete Shift:=xlToLeft
Columns("BH:BH").Select
Selection.Delete Shift:=xlToLeft
Columns("BK:BK").Select
Selection.Delete Shift:=xlToLeft
Columns("BN:BN").Select
Selection.Delete Shift:=xlToLeft
Columns("BQ:BQ").Select
Selection.Delete Shift:=xlToLeft
Columns("BT:BT").Select
Selection.Delete Shift:=xlToLeft
Columns("BW:BW").Select
Selection.Delete Shift:=xlToLeft
Columns("BZ:BZ").Select
Selection.Delete Shift:=xlToLeft
Columns("CC:CC").Select
Selection.Delete Shift:=xlToLeft
Columns("CF:CF").Select
Selection.Delete Shift:=xlToLeft
Columns("CI:CI").Select
Selection.Delete Shift:=xlToLeft
Columns("CL:CL").Select
Selection.Delete Shift:=xlToLeft
Columns("CO:CO").Select
Selection.Delete Shift:=xlToLeft
Columns("CR:CR").Select
Selection.Delete Shift:=xlToLeft
Columns("CU:CU").Select
Selection.Delete Shift:=xlToLeft
Columns("CX:CX").Select
Selection.Delete Shift:=xlToLeft
Columns("DA:DA").Select
Selection.Delete Shift:=xlToLeft
Columns("DD:DD").Select
Selection.Delete Shift:=xlToLeft
Columns("DG:DG").Select
Selection.Delete Shift:=xlToLeft
Columns("DJ:DJ").Select
Selection.Delete Shift:=xlToLeft
Columns("DM:DM").Select
Selection.Delete Shift:=xlToLeft
Columns("DP:DP").Select
Selection.Delete Shift:=xlToLeft
Columns("DS:DS").Select
Selection.Delete Shift:=xlToLeft
Columns("DV:DV").Select
Selection.Delete Shift:=xlToLeft
Columns("DY:DY").Select
Selection.Delete Shift:=xlToLeft
Columns("EB:EB").Select
Selection.Delete Shift:=xlToLeft
Columns("EE:EE").Select
Selection.Delete Shift:=xlToLeft
Columns("EH:EH").Select
Selection.Delete Shift:=xlToLeft
Columns("EK:EK").Select
Selection.Delete Shift:=xlToLeft
Columns("EN:EN").Select
Selection.Delete Shift:=xlToLeft
Columns("EQ:EQ").Select
Selection.Delete Shift:=xlToLeft
Columns("ET:ET").Select
Selection.Delete Shift:=xlToLeft
Columns("EW:EW").Select
Selection.Delete Shift:=xlToLeft
Columns("EY:EY").Select
Selection.Delete Shift:=xlToLeft
Columns("FB:FB").Select
Selection.Delete Shift:=xlToLeft
Columns("FE:FE").Select
Selection.Delete Shift:=xlToLeft
Columns("FH:FH").Select
Selection.Delete Shift:=xlToLeft
Columns("FK:FK").Select
Selection.Delete Shift:=xlToLeft
Columns("FN:FN").Select
Selection.Delete Shift:=xlToLeft
Columns("FQ:FQ").Select
Selection.Delete Shift:=xlToLeft
Columns("FT:FT").Select
Selection.Delete Shift:=xlToLeft
Columns("FW:FW").Select
Selection.Delete Shift:=xlToLeft
Columns("FZ:FZ").Select
Selection.Delete Shift:=xlToLeft
Columns("GC:GC").Select
Selection.Delete Shift:=xlToLeft
Columns("GF:GF").Select
Selection.Delete Shift:=xlToLeft
Columns("GI:GI").Select
Selection.Delete Shift:=xlToLeft
Columns("GL:GL").Select
Selection.Delete Shift:=xlToLeft
Columns("GO:GO").Select
Selection.Delete Shift:=xlToLeft
Columns("GR:GR").Select
Selection.Delete Shift:=xlToLeft
Columns("GU:GU").Select
Selection.Delete Shift:=xlToLeft
Columns("GX:GX").Select
Selection.Delete Shift:=xlToLeft
Columns("HA:HA").Select
Selection.Delete Shift:=xlToLeft
Columns("HD:HD").Select
Selection.Delete Shift:=xlToLeft
Columns("HG:HG").Select
Selection.Delete Shift:=xlToLeft
Columns("HJ:HJ").Select
Selection.Delete Shift:=xlToLeft
Columns("HM:HM").Select
Selection.Delete Shift:=xlToLeft
Columns("HP:HP").Select
Selection.Delete Shift:=xlToLeft
Columns("HS:HS").Select
Selection.Delete Shift:=xlToLeft
Columns("HV:HV").Select
Selection.Delete Shift:=xlToLeft
Columns("HY:HY").Select
Selection.Delete Shift:=xlToLeft
Columns("IB:IB").Select
Selection.Delete Shift:=xlToLeft
Columns("IE:IE").Select
Selection.Delete Shift:=xlToLeft
Columns("IH:IH").Select
Selection.Delete Shift:=xlToLeft
Columns("IK:IK").Select
Selection.Delete Shift:=xlToLeft
Columns("IN:IN").Select
Selection.Delete Shift:=xlToLeft
Columns("IQ:IQ").Select
Selection.Delete Shift:=xlToLeft
Columns("IT:IT").Select
Selection.Delete Shift:=xlToLeft
Columns("IW:IW").Select
Selection.Delete Shift:=xlToLeft
Columns("IZ:IZ").Select
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.ColumnWidth = 10.86
Columns("F:F").Select
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("I:I").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("L:L").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveWindow.SmallScroll ToRight:=7
Columns("O:O").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("R:R").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("U:U").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveWindow.SmallScroll ToRight:=12
Columns("X:X").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("AA:AA").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("AD:AD").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("AG:AG").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("AJ:AJ").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveWindow.SmallScroll ToRight:=10
Columns("AM:AM").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("AP:AP").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("AS:AS").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("AV:AV").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveWindow.SmallScroll ToRight:=11
Columns("AY:AY").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("BB:BB").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("BE:BE").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveWindow.ScrollColumn = 1
Range("C2").Select
ActiveCell.FormulaR1C1 = "T1"
Range("C2:E2").Select
Selection.AutoFill Destination:=Range("C2:BD2"), Type:=xlFillDefault
Range("C2:BD2").Select
Columns("BE:BE").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("BH:BH").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("BB2:BD2").Select
Selection.AutoFill Destination:=Range("BB2:BJ2"), Type:=xlFillDefault
Range("BB2:BJ2").Select
ActiveWindow.ScrollColumn = 1
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Range("C3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=D1"
Range("C3").Select
Selection.Copy
Range("D1").Select
Application.CutCopyMode = False
Selection.Copy
Range("C3").Select
ActiveSheet.Paste
Cells.Select
Selection.Font.Bold = False
With Selection
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
With Selection
    .HorizontalAlignment = xlGeneral
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
Range("G1").Select
Selection.Copy
Range("F3").Select
ActiveSheet.Paste
Range("J1").Select
Application.CutCopyMode = False
Selection.Copy
Range("I3").Select
ActiveSheet.Paste
Range("M1").Select
Application.CutCopyMode = False
Selection.Copy
Range("L3").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll ToRight:=8
Range("P1").Select
Application.CutCopyMode = False
Selection.Copy
Range("O3").Select
ActiveSheet.Paste
Range("S1").Select
Application.CutCopyMode = False
Selection.Copy
Range("R3").Select
ActiveSheet.Paste
Range("V1").Select
Application.CutCopyMode = False
Selection.Copy
Range("U3").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll ToRight:=9
Range("Y1").Select
Application.CutCopyMode = False
Selection.Copy
Range("X3").Select
ActiveSheet.Paste
Range("AB1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AA3").Select
ActiveSheet.Paste
Range("AE1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AD3").Select
ActiveSheet.Paste
Range("AH1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AG3").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll ToRight:=8
Range("AK1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AJ3").Select
ActiveSheet.Paste
Range("AN1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AM3").Select
ActiveSheet.Paste
Range("AQ1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AP3").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll ToRight:=12
Range("AT1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AS3").Select
ActiveSheet.Paste
Range("AW1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AV3").Select
ActiveSheet.Paste
Range("AZ1").Select
Application.CutCopyMode = False
Selection.Copy
Range("AY3").Select
ActiveSheet.Paste
Range("BC1").Select
Application.CutCopyMode = False
Selection.Copy
Range("BB3").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll ToRight:=7
Range("BF1").Select
Application.CutCopyMode = False
Selection.Copy
Range("BE3").Select
ActiveSheet.Paste
Range("BI1").Select
Application.CutCopyMode = False
Selection.Copy
Range("BH3").Select
ActiveSheet.Paste
ActiveWindow.ScrollColumn = 1
Range("C3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("C3:C3499"), Type:=xlFillDefault
Range("C3:C3499").Select
ActiveWindow.ScrollRow = 1
Columns("D:E").Select
Selection.EntireColumn.Hidden = True
Range("F3").Select
Selection.AutoFill Destination:=Range("F3:F3499")
Range("F3:F3499").Select
Columns("G:H").Select
Selection.EntireColumn.Hidden = True
Range("I3").Select
Selection.AutoFill Destination:=Range("I3:I3499")
Range("I3:I3499").Select
Columns("J:K").Select
Selection.EntireColumn.Hidden = True
Range("L3").Select
Selection.AutoFill Destination:=Range("L3:L3499")
Range("L3:L3499").Select
Columns("M:N").Select
Selection.EntireColumn.Hidden = True
Range("O3").Select
Selection.AutoFill Destination:=Range("O3:O3499")
Range("O3:O3499").Select
Columns("P:Q").Select
Selection.EntireColumn.Hidden = True
Range("R3").Select
Selection.AutoFill Destination:=Range("R3:R3499")
Range("R3:R3499").Select
Columns("S:T").Select
Selection.EntireColumn.Hidden = True
Range("U3").Select
Selection.AutoFill Destination:=Range("U3:U3499")
Range("U3:U3499").Select
Columns("V:W").Select
Selection.EntireColumn.Hidden = True
Range("X3").Select
Selection.AutoFill Destination:=Range("X3:X3499")
Range("X3:X3499").Select
Columns("Y:Z").Select
Selection.EntireColumn.Hidden = True
ActiveWindow.SmallScroll ToRight:=4
Range("AA3").Select
Selection.AutoFill Destination:=Range("AA3:AA3499")
Range("AA3:AA3499").Select
Columns("AB:AC").Select
Selection.EntireColumn.Hidden = True
Range("AD3").Select
Selection.AutoFill Destination:=Range("AD3:AD3499")
Range("AD3:AD3499").Select
Columns("AE:AF").Select
Selection.EntireColumn.Hidden = True
Range("AG3").Select
Selection.AutoFill Destination:=Range("AG3:AG3499")
Range("AG3:AG3499").Select
Columns("AH:AI").Select
Selection.EntireColumn.Hidden = True
Range("AJ3").Select
Selection.AutoFill Destination:=Range("AJ3:AJ3499"), Type:=xlFillDefault
Range("AJ3:AJ3499").Select
ActiveWindow.ScrollRow = 1
Columns("AK:AL").Select
Selection.EntireColumn.Hidden = True
Range("AM3").Select
Selection.AutoFill Destination:=Range("AM3:AM3499"), Type:=xlFillDefault
Range("AM3:AM3499").Select
ActiveWindow.ScrollRow = 1
Columns("AN:AO").Select
Selection.EntireColumn.Hidden = True
Range("AP3").Select
Selection.AutoFill Destination:=Range("AP3:AP3499"), Type:=xlFillDefault
Range("AP3:AP3499").Select
ActiveWindow.ScrollRow = 1
Columns("AQ:AR").Select
Selection.EntireColumn.Hidden = True
Range("AS3").Select
Selection.AutoFill Destination:=Range("AS3:AS3499"), Type:=xlFillDefault
Range("AS3:AS3499").Select
ActiveWindow.ScrollRow = 1
Columns("AT:AU").Select
Selection.EntireColumn.Hidden = True
Range("AV3").Select
Selection.AutoFill Destination:=Range("AV3:AV3499"), Type:=xlFillDefault
Range("AV3:AV3499").Select
ActiveWindow.ScrollColumn = 31
ActiveWindow.ScrollRow = 1
Columns("AW:AX").Select
Selection.EntireColumn.Hidden = True
Range("AY3").Select
Selection.AutoFill Destination:=Range("AY3:AY3499"), Type:=xlFillDefault
Range("AY3:AY3499").Select
ActiveWindow.ScrollRow = 1
Columns("AZ:BA").Select
Selection.EntireColumn.Hidden = True
Range("BB3").Select
Selection.AutoFill Destination:=Range("BB3:BB3499"), Type:=xlFillDefault
Range("BB3:BB3499").Select
ActiveWindow.ScrollRow = 1
Columns("BC:BD").Select
Selection.EntireColumn.Hidden = True
Range("BE3").Select
Selection.AutoFill Destination:=Range("BE3:BE3499"), Type:=xlFillDefault
Range("BE3:BE3499").Select
ActiveWindow.ScrollRow = 1
Columns("BF:BG").Select
Selection.EntireColumn.Hidden = True
Range("BH3").Select
Selection.AutoFill Destination:=Range("BH3:BH3499"), Type:=xlFillDefault
Range("BH3:BH3499").Select
ActiveWindow.ScrollRow = 1

MsgBox ActiveWorkbook.Worksheets(I).Name

     Next I

  End Sub

1 个答案:

答案 0 :(得分:0)

这样修改:

 For I = 1 To WS_Count
    ActiveWorkbook.Worksheets(I).Activate

这是因为未经验证的范围对象(如CellsRange)始终隐式属于活动表,因此为了使此代码在所有工作表上运行,您只需按顺序激活每张纸。 (这在技术上并不正确,但它是解决您当前问题的最简单的解决方案。)

您的其余代码不需要立即更改,但您可以从中受益匪浅:

How to avoid using Select in Excel VBA macros

避免依赖SelectActivate方法。这些只是让你模拟按键/鼠标点击,这对于学习和记录宏是可以的,但是面向对象编程的目的是直接使用对象模型。欢呼声。

相关问题