Deleting block of rows error - sample code included

marchantvii

New Member
Joined
Jun 2, 2016
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

I have the following code below that was running fine until I came to use it this month.

I use this to search through col A on an imported spreadsheet for anything like "DATE" and delete that row and the next 7 rows below. This block repeats roughly every 24/25 rows.

I am currently getting the following error message when running:

VBA Code:
Sub GRNI_002_DeleteHeaderRows()

    Range("A1").Select

    Dim lastRow As Long
    Dim CurrentRow As Long
    lastRow = Range("A1").End(xlDown).Row
    CurrentRow = 1

    Do While CurrentRow <= lastRow

        If Range("A" & CurrentRow).Value Like "*DATE*" Then
            Range("A" & CurrentRow).EntireRow.Delete
            Range("A" & CurrentRow).EntireRow.Delete
            Range("A" & CurrentRow).EntireRow.Delete
            Range("A" & CurrentRow).EntireRow.Delete
            Range("A" & CurrentRow).EntireRow.Delete
            Range("A" & CurrentRow).EntireRow.Delete
            Range("A" & CurrentRow).EntireRow.Delete
            Range("A" & CurrentRow).EntireRow.Delete
            lastRow = lastRow - 8
            CurrentRow = CurrentRow - 8
            End If

        ' put the pointer to the next row we want to evaluate
        CurrentRow = CurrentRow + 1

    Loop

End Sub

The data is roughly 1500 rows long.



Any help would be greatly appreciated.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
In any case, here is another code to try with a copy of your workbook.

VBA Code:
Sub DelHdrRws()
  Dim rFound As Range
  
  Application.ScreenUpdating = False
  Set rFound = Columns("A").Find(What:="DATE", LookAt:=xlPart, MatchCase:=True)
  Do Until rFound Is Nothing
    rFound.Resize(8).EntireRow.Delete
    Set rFound = Columns("A").Find(What:="DATE", LookAt:=xlPart, MatchCase:=True)
  Loop
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
In any case, here is another code to try with a copy of your workbook.

VBA Code:
Sub DelHdrRws()
  Dim rFound As Range
 
  Application.ScreenUpdating = False
  Set rFound = Columns("A").Find(What:="DATE", LookAt:=xlPart, MatchCase:=True)
  Do Until rFound Is Nothing
    rFound.Resize(8).EntireRow.Delete
    Set rFound = Columns("A").Find(What:="DATE", LookAt:=xlPart, MatchCase:=True)
  Loop
  Application.ScreenUpdating = True
End Sub
Thanks Peter for your reply - your code worked a treat.

For reference, the error occured on the following line:

VBA Code:
If Range("A" & CurrentRow).Value Like "*DATE*" Then

With the following error attached:

Run-time error '10041:

Method 'Range' of object'_Global' failed
 
Upvote 0
With the following error attached:

Run-time error '10041:

Method 'Range' of object'_Global' failed
I suspect there was a 'DATE' row near the top of the worksheet so that CurrentRow = CurrentRow - 8 produced a negative value for CurrentRow so
Range("A" & CurrentRow) would not be a valid cell reference.

Thanks Peter for your reply - your code worked a treat.
You're welcome. Thanks for the confirmation. :)
 
Upvote 0
I suspect there was a 'DATE' row near the top of the worksheet so that CurrentRow = CurrentRow - 8 produced a negative value for CurrentRow so
Range("A" & CurrentRow) would not be a valid cell reference.


You're welcome. Thanks for the confirmation. :)
Right - thanks for confirming and the speedy reply :) Have a great day!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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