0

I'm trying to loop through all sheets in a workbook, starting with the 4th tab. This is what I have, but for some reason it only works on the 4th tab, and doesn't continue with the rest. Any ideas?

Sub Step13()

Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
  If sheet.Index > 3 Then

'my code here 
Columns("A").Replace "^", vbNullString, xlPart, xlByRows, True
Range("AA1").Value = "Function"
Dim LastRowColumnA As Long
LastRowColumnA = Cells(Rows.Count, 1).End(xlUp).Row
Range("AA2:AA" & LastRowColumnA).FormulaR1C1 = "=ISNUMBER(MATCH(RC[-26],EarningsDates!C[-26],0))"
Columns(27).Value = Columns(27).Value

  End If
Next

End Sub
Gitty
  • 166
  • 8

3 Answers3

0

Below is the corrected code. Works perfectly.

Sub Step13()

Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
  If sheet.Index > 3 Then

sheet.Activate

'my code here 
ActiveSheet.Columns("A").Replace "^", vbNullString, xlPart, xlByRows, True
ActiveSheet.Range("AA1").Value = "Function"
Dim LastRowColumnA As Long
LastRowColumnA = Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Range("AA2:AA" & LastRowColumnA).FormulaR1C1 = "=ISNUMBER(MATCH(RC[-26],EarningsDates!C[-26],0))"
ActiveSheet.Columns(27).Value = Columns(27).Value

  End If
Next

End Sub
Gitty
  • 166
  • 8
0

I think you will find the below code enhances the performance of your routine for two reasons:

  1. It works directly with the needed object. (and doesn't activate each sheet each time, which is not necessary

  2. It only works specifically with cells with data (and not entire columns, most rows of which are not necessary.

Code:

Option Explicit

Sub Step13()

    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets

        If ws.Index > 3 Then

            With ws

                Dim LastRowColumnA As Long
                LastRowColumnA = .Cells(.Rows.Count, 1).End(xlUp).Row

                'my code here
                .Range("A1:A" & LastRowColumnA).Replace "^", vbNullString, xlPart, xlByRows, True

                .Range("AA1").Value = "Function"
                With .Range("AA2:AA" & LastRowColumnA)
                    .FormulaR1C1 = "=ISNUMBER(MATCH(RC[-26],EarningsDates!C[-26],0))"
                    .Value = .Value
                End With

            End With

        End If

    Next

End Sub
Scott Holtzman
  • 27,099
  • 5
  • 37
  • 72
0

The reason you were having issues is that you had not fully qualified your ranges, and your code is only working on the active sheet. The answer to your problem is to fully qualify any reference to a range to ensure the code properly understands which cell, range, sheet or workbook you are referring to.

Below is corrected code from Gilty to remove bad programming practices. Not tested.

Option Explicit ' <-- always include this at the top of modules.

' Proper indenting helps make code readable and maintainable.
Sub Step13()
    Dim sheet As Worksheet
    For Each sheet In ActiveWorkbook.Worksheets
        If sheet.Index > 3 Then
        ' Do not use .Select or .Activate unless you want the user to see something.
        ' Fully qualify all ranges.
            sheet.Columns("A").Replace "^", vbNullString, xlPart, xlByRows, True
            sheet.Range("AA1").Value = "Function"
            Dim LastRowColumnA As Long
            LastRowColumnA = sheet.Cells(sheet.Rows.Count, 1).End(xlUp).Row '<-- Fully qualify ALL references to ranges.
            sheet.Range("AA2:AA" & LastRowColumnA).FormulaR1C1 = "=ISNUMBER(MATCH(RC[-26],EarningsDates!C[-26],0))"
            sheet.Columns(27).Value = sheet.Columns(27).Value '<-- Fully qualify ALL references to ranges.
        End If
    Next
End Sub

Now this can be a little neater by using With.

Option Explicit

Sub Step13()
    Dim sheet As Worksheet
    For Each sheet In ActiveWorkbook.Worksheets
        With sheet
            If .Index > 3 Then
                .Columns("A").Replace "^", vbNullString, xlPart, xlByRows, True
                .Range("AA1").Value = "Function"
                Dim LastRowColumnA As Long
                LastRowColumnA = .Cells(.Rows.Count, 1).End(xlUp).Row 
                .Range("AA2:AA" & LastRowColumnA).FormulaR1C1 = "=ISNUMBER(MATCH(RC[-26],EarningsDates!C[-26],0))"
                .Columns(27).Value = .Columns(27).Value 
            End If
        End With
    Next
End Sub
AJD
  • 2,400
  • 2
  • 12
  • 22