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