i am trying to write a macro to remove some page headers. i need to find each occurance of some text and then remove the row it is on and the next 8 rows. the macro below works but it finishes with an error and the line highlighted in yellow in Visual Basic is Loop While Not c Is Nothing And c.Address <> firstAddress
With ActiveSheet.UsedRange.Cells
Set c = .Find(What:="Micro Syste", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Select
ActiveCell.Rows("1:9").EntireRow.Select
Selection.Delete Shift:=xlUp
Set c = .FindNext()
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
i don't fully understand the logic of the macro because i have created it by copying from examples even though i have tried looking things up in the VB help programme. my guess of what the code does are the comments in brown:
With ActiveSheet.UsedRange.Cells tells it to only look at cells with something in
Set c = .Find(What:="Micro Syste", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False) sets a variable called c so where c is found in the code it will look for "Micro Syste" text.
If Not c Is Nothing
firstAddress = c.Address this creates a variable called firstAddress with the cell reference of the text found by c?
Do
c.Select makes the cell found active
ActiveCell.Rows("1:9").EntireRow.Select
Selection.Delete Shift:=xlUp selects 9 rows and deletes them
Set c = .FindNext() resets the variable c to the cell reference of the next occurance of the text
Loop While Not c Is Nothing And c.Address <> firstAddress keeping looping is all i understand here![]()
End If
End With
Bookmarks