VBA to delete variable (n-Rows) from top lines of each Worksheet within Workbook

Onceapirate

New Member
Joined
Oct 13, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, (complete novice here)

I'd appreciate help, I've struggled on my own for too long today to make progress...

What I have: I've a Workbook containing multiple (almost 30) Worksheets - each sheet having a different Name but having somewhat different Header Structure. Common to all of these is that the client info I need starts below a cell in Column A that always contains text "Header Number". Sometimes this appears in Row 11 and sometimes in Row 14.

What I need: I want to delete all Rows from A1 down to and including the Row containing text "Header Number", so that each Worksheet will only have client info and No Headers. I am trying to get VBA code to loop through ALL sheets in the Workbook and delete these top 11 or top 14 Rows.

I have tried (below) which will work for the active sheet I am viewing, but I cannot get it to loop through all the other Sheets in the Workbook:

'--------------
Sub DelRows_down_to_Header_Number()
Dim r As Range
Dim ws As Worksheet

For Each Sh In ActiveWorkbook.Worksheets

Set r = Range("A1", "A" & Rows.Count).Find(What:="Header Number", LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext, MatchCase:=False)

If Not r Is Nothing Then
Range("A1:A" & r.Row - 0).EntireRow.Delete

Else
MsgBox "No cell matching criteria found!"

End If
Next Sh

End Sub
'-----------------

Any help greatly appreciated!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Welcome to the forum,

Couple of options below:
VBA Code:
Sub test()
    Dim ws As Worksheet
    Dim fRow As Long
   
    On Error Resume Next
        For Each ws In Sheets
            fRow = ws.UsedRange.Find("Header Number", , , xlWhole).Row
            ws.Rows("1:" & fRow).Delete
            fRow = 0
        Next ws
    On Error GoTo 0
End Sub

VBA Code:
Sub test2()
    Dim fRng As Range
    Dim ws As Worksheet
   
    For Each ws In Sheets
        Set fRng = ws.UsedRange.Find("Header Number", , , xlWhole)
        If Not fRng Is Nothing Then
            ws.Rows("1:" & fRng.Row).Delete
            Set fRng = Nothing
        End If
    Next ws
End Sub
 
Upvote 1
Solution
Welcome to the forum,

Couple of options below:
VBA Code:
Sub test()
    Dim ws As Worksheet
    Dim fRow As Long
  
    On Error Resume Next
        For Each ws In Sheets
            fRow = ws.UsedRange.Find("Header Number", , , xlWhole).Row
            ws.Rows("1:" & fRow).Delete
            fRow = 0
        Next ws
    On Error GoTo 0
End Sub

VBA Code:
Sub test2()
    Dim fRng As Range
    Dim ws As Worksheet
  
    For Each ws In Sheets
        Set fRng = ws.UsedRange.Find("Header Number", , , xlWhole)
        If Not fRng Is Nothing Then
            ws.Rows("1:" & fRng.Row).Delete
            Set fRng = Nothing
        End If
    Next ws
End Sub
Thank you so much - That has worked perfectly!

(hope I can pay it forward as my skills improve)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top