I have a few different subs that I incorprated into one and need to have it loop thru all the worksheets, but the code just repeats or attempts to run again on the first sheet until it errors out, never shifting to the next sheet.
I'm sure the problem is obvious, esp. to a more experienced eye. Can someone kindly take a look and see what the problem is. See attachment if needed...
Thanks a lot!
Sub ProjectFormatter()
Application.ScreenUpdating = False
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
'concatenate "Project" & "Cost" cells
Dim cell As Range
Dim sStart As String
Dim rng As Range
With Columns(1)
Set cell = .Find("Project")
If Not cell Is Nothing Then
If cell.Row > 2 Then
sStart = cell.Address
Do
cell.Offset(0, 0).Value = cell.Offset(0, 0).Value & " - " & _
cell.Offset(1, 0).Value
If rng Is Nothing Then
Set rng = cell.Offset(1, 0)
Else
Set rng = Union(rng, cell.Offset(1, 0))
End If
Set cell = .FindNext(cell)
Loop Until cell Is Nothing Or cell.Address = sStart
End If
End If
End With
If Not rng Is Nothing Then rng.EntireRow.Delete
'move "Project" cells up to same row as Phase (in col. D)
For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
If Len(WorksheetFunction.Substitute(c, "Project", "")) <> Len(c) Then
c.Cut c.Offset(-1, 2)
c.Offset(1, 0).EntireRow.Delete
End If
Next c
'------------------------------------------------------------
Dim LastRow As Long
Dim aCount As Long
Dim iCount As Long
Dim I As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A3").Select
Do While ActiveCell.Row < LastRow
'//Must be a value in col A
If InStr(1, ActiveCell.Offset(0, 2), "Project") Then
'//When project line move down to first task
ActiveCell.Offset(1, 0).Select
Do While InStr(1, ActiveCell.Offset(0, 2), "Project") = 0
'//move down task rows until next project found
If ActiveCell.Row > LastRow Then
'//trap for when at end of dataset
Exit Do
Else
'//actually move down and count rows moved
ActiveCell.Offset(1, 0).Select
aCount = aCount + 1
End If
Loop
'//determine if any rows need inserting
iCount = (10 - aCount)
If iCount > 0 Then
'//don't insert when <= 0
For I = 1 To iCount
ActiveCell.EntireRow.Insert
ActiveCell.Offset(1, 0).Select
Next I
'//shift last row count by inserted rows amount
LastRow = LastRow + iCount
End If
'//reset count
aCount = 0
End If
Loop
Application.ScreenUpdating = True
Next ws
End Sub