我修改了在SO上找到的现有代码来循环遍历工作表。但是,代码仅重复执行模板工作表上的调用宏功能。起始工作表将在sheet4(因此+3)。模板之间有两个隐藏的工作表,我需要编写代码才能启动。我在这做错了什么?我是VBA的新手。
How can I loop through a subset of worksheets?
Sub WorkbookLoop()
'LoopThruFlaggedSheets
' determine current bounds
Dim StartIndex As Integer
Dim EndIndex As Integer
Dim LoopIndex As Integer
StartIndex = Sheets("Template for AM").Index + 3
EndIndex = Sheets("End").Index - 1
For LoopIndex = StartIndex To EndIndex
Call insertheading
Next LoopIndex
End Sub
Sub insertheading()
'
' snippet of insertheading Macro
ActiveSheet.Activate
Rows("1:6").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Carrier"
Range("A2").Select
ActiveCell.FormulaR1C1 = "Account Manager(s)"
Range("A3").Select
ActiveCell.FormulaR1C1 = "Email1"
Range("A4").Select
ActiveCell.FormulaR1C1 = "Email2"
Range("A5").Select
ActiveCell.FormulaR1C1 = "Case ID"
Range("A1").Select
End Sub
已解决:下面的工作代码循环遍历每个工作表并插入标题
Sub LoopHeader4252014()
Dim sh As Worksheet
Dim sCurrentSheet As String
sCurrentSheet = ActiveSheet.Name
Application.ScreenUpdating = False
For Each sh In ThisWorkbook.Worksheets
sh.Activate
'Ignore sheet names to ignore
If sh.Name <> "Sheet1" And sh.Name <> "Sheet2" And sh.Name <> "Sheet3" Then
ActiveSheet.Activate
Rows("1:6").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "SheetHeader1"
Range("A2").Select
ActiveCell.FormulaR1C1 = "SheetHeader2"
Range("A3").Select
ActiveCell.FormulaR1C1 = "SheetHeader3"
Range("A4").Select
ActiveCell.FormulaR1C1 = "SheetHeader4"
Range("A5").Select
ActiveCell.FormulaR1C1 = "SheetHeader5"
Range("B1").Select
ActiveCell.FormulaR1C1 = "=R[7]C[-1]"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('AM List'!R2C1:R1083C8,MATCH(R[-1]C,CARRIER,0),1)"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B3"), Type:=xlFillDefault
Range("B2:B3").Select
Range("B3").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('AM List'!R2C2:R1083C8,MATCH(R[-1]C,CARRIER,0),7)"
Range("B3").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('AM List'!R2C2:R1083C8,MATCH(R[-2]C,CARRIER,0),7)"
Range("B4").Select
ActiveCell.FormulaR1C1 = _
"=IF(INDEX('AM List'!R2C2:R1083C9,MATCH(R[-3]C,CARRIER,0),8)=""No 2nd AM"","""",(INDEX('AM List'!R2C2:R1083C9,MATCH(R[-3]C,CARRIER,0),8)))"
Range("B5").Select
ActiveCell.FormulaR1C1 = "='Template'!RC"
Range("B1:B5").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("A7").Select
ActiveCell.FormulaR1C1 = "ColumnHeader1"
Range("B7").Select
ActiveCell.FormulaR1C1 = "ColumnHeader2"
Range("C7").Select
ActiveCell.FormulaR1C1 = "ColumnHeader3"
Range("D7").Select
ActiveCell.FormulaR1C1 = "ColumnHeader4"
Columns("D:D").Select
Application.CutCopyMode = False
Selection.NumberFormat = "m/d/yyyy"
Range("E7").Select
ActiveCell.FormulaR1C1 = "Reversal Auth #"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Note"
Range("E1").Select
Selection.Font.Bold = True
Columns("A:E").Select
Columns.EntireColumn.AutoFit
Range("A7").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("A1").Select
End If
Next sh
Worksheets(sCurrentSheet).Activate
Application.Range("A1").Select
ExitHandler:
Exit Sub
ErrHandler:
MsgBox (Err.Number & vbCrLf & Err.Description)
GoTo ExitHandler
End Sub