0

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
Community
  • 1
  • 1
Bentley12
  • 1
  • 1
  • The issue here is "Insertheading" works on the currently active sheet, and you never change what that is. – Rejected Apr 22 '14 at 20:52
  • I cannot specify the Sheet name in the Macro insertheading because it could vary. I thought the StartIndex code was saying to perform the insertheading macro on the 4th worksheet. There's a preceeding Macro that filters by carrier and creates a new worksheet within the workbook for each unique carrier found and then renames the worksheet with the carrier name. I would then like to loop through those worksheets and insert the header on each worksheet created. I hope this helps explain more of what I'm trying to accomplish. Thank you! – Bentley12 Apr 24 '14 at 12:54
  • `Startindex` and `EndIndex` are integers. All the Loop is doing is calling the `insertheading` method. This method just operates on the active page, which will always been the same page. Either set the desired worksheet before calling `insertheading`, or pass `LoopIndex` to `insertheading` and have it do it in there. – Rejected Apr 24 '14 at 20:40
  • Thank you for your input. I took a different approach to accomplish what I needed. I'm sure it isn't the most efficient way but IT WORKS! – Bentley12 Apr 25 '14 at 15:22

0 Answers0