I modified an existing code found on SO to loop through worksheets. However, the code is only performing the Call macro function on the Template worksheet repeatedly. The starting worksheet will be at sheet4 (Hence +3). There are two hidden worksheets between the template and where I need to code to start. What am I doing wrong here? I'm a VBA novice.
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
SOLVED: Working Code below loops through each worksheet and inserts headers
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