Edited. Trying to loop my current VBA code through entire excel workbook, have tried For Each ws In Sheets ws.Activate but doesn't work, it doesn't loop through the entire workbook, but only for the sheet I'm on. Any help appreciated!
Sub InsertRows()
Dim ws As Worksheet
Dim rng As Range
Dim FirstRange As Excel.Range
For Each ws In Sheets
ws.Activate
Set rng = ActiveSheet.Cells.Find(What:="*XXX*", MatchCase:=False, Lookat:=xlWhole)
Do While Not rng Is Nothing
If FirstRange Is Nothing Then
Set FirstRange = rng
Else
If rng.Address = FirstRange.Address Then
Exit Do
End If
End If
If WorksheetFunction.CountBlank(rng.Offset(1).EntireRow) <> Columns.Count Then
rng.Offset(1).EntireRow.Insert
rng.Offset(1).EntireRow.Insert
End If
Set rng = ActiveSheet.Cells.FindNext(After:=rng.Cells(1))
Loop
Next ws
End Sub
For Each ws In Sheets
in your code. Also, "..doesn't work* doesn't give us any information on what the actual issue is. What happens when you run the loop? it never steps into the loop? It throws an error? if so, what error does it throw and on which line? – ZacLoop
but as @Zac points out there is noFor
. Also, if you want to loop through all your sheets only to process some data there is no need to activate them. Just refer to each one usingSet rng = ws.Cells
instead ofSet rng = ActiveSheet.Cells
. – romulax14Set rng = ws.Cells
results in object required error – yammy