Problem with macro

pholt33

Board Regular
Joined
Jan 4, 2005
Messages
202
Office Version
  1. 365
Platform
  1. Windows
I am trying to set up a macro to format a report that I download. Most of the formatting is simply deleting rows that contain certain words or blank rows. When I run the macro, it deletes the rows with "amount" in them, but doesnt delete the rows that are blank. But if I run the macro again, it deletes most of the empty rows. Ultimately, I have to run the macro 3 times in order to get everything formatted properly.

How can I get this thing to work properly?

Heres the code:

For i = 1 To Range("A65536").End(xlUp).Row
If (Cells(i, 2) = ("Amount")) _
Then
Rows(i).EntireRow.Delete
End If
Next i

For i = 1 To Range("A65536").End(xlUp).Row
If (Cells(i, 2) = ("")) _
Then
Rows(i).EntireRow.Delete
End If
Next i
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I use this type of structure.

Code:
 Set TestRange = Intersect(Range("A:A"), ActiveSheet.UsedRange)
        lastrow = TestRange.Cells(TestRange.Cells.Count).Row 'get last row number in the range
        firstrow = TestRange.Cells(1).Row 'get the first row number in the range
For i = lastrow  To firstrow 
If Cells(i, 2).value = "Amount" or cells(i, 2).value is empty Then 
Rows(i).EntireRow.Delete 
End If 
Next i
 
Upvote 0
I use this type of structure.

Code:
 Set TestRange = Intersect(Range("A:A"), ActiveSheet.UsedRange)
        lastrow = TestRange.Cells(TestRange.Cells.Count).Row 'get last row number in the range
        firstrow = TestRange.Cells(1).Row 'get the first row number in the range
For i = lastrow  To firstrow step -1
If Cells(i, 2).value = "Amount" or cells(i, 2).value is empty Then 
Rows(i).EntireRow.Delete 
End If 
Next i
 
Upvote 0
What error are you getting. Or is it just not doing anything??

I did make a typo error before so try this one

Code:
Set TestRange = Intersect(Range("A:A"), ActiveSheet.UsedRange) 
        lastrow = TestRange.Cells(TestRange.Cells.Count).Row 'get last row number in the range 
        firstrow = TestRange.Cells(1).Row 'get the first row number in the range 
For i = lastrow  To firstrow step -1
If Cells(i, 2).value = "Amount" or cells(i, 2).value is empty Then 
Rows(i).EntireRow.Delete 
End If 
Next i
 
Upvote 0
With your revised version I get

Run time error 424 "Object required" and it highlights the "If cells..." line
 
Upvote 0
OK I have narrowed it down.

Try this.

Sub test()
Set TestRange = Intersect(Range("A:A"), ActiveSheet.UsedRange)
lastrow = TestRange.Cells(TestRange.Cells.Count).Row 'get last row number in the range
firstrow = TestRange.Cells(1).Row 'get the first row number in the range
For i = lastrow To firstrow Step -1
If Cells(i, 2).Value = "Amount" Or Cells(i, 2).Value = Empty Then
Rows(i).EntireRow.Delete
End If
Next
End Sub
 
Upvote 0
With one very small change just to make sure it didnt delete the top few lines, that last version worked perfectly!

Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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