Delete blank rows or skip to next if none

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
150
Hello, I need help expanding my VBA code to include if there are not blank rows then skip to next step of the macro code.

Here's what I have, but if there are no blank rows, then it bugs.

Excel Formula:
Range("A2:A15000").SpecialCells(xlCellTypeBlanks).EntireRow.Delete


Thanks!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
One simple way is to ignore the error, i.e.
VBA Code:
On Error Resume Next
Range("A2:A15000").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
 
Upvote 0
Solution
Pretty sure that go to 0 disables error handling in the currently running procedure. Might want to be aware of that as code could fail in unexpected ways. I'm on a cruise ship so I must leave it to you to verify.
 
Upvote 0
Pretty sure that go to 0 disables error handling in the currently running procedure. Might want to be aware of that as code could fail in unexpected ways. I'm on a cruise ship so I must leave it to you to verify.
Not quite. When you use a statement like "On Error Resume Next" (which ignores an error), using "On Error Goto 0" after it restores it back to the default setting.
By surrounding that one line of existing code with those two "On Error..." lines, it will only ignore errors from that one line of code.
See: VBA On Error Resume Next or Goto 0 - Automate Excel
 
Upvote 0
While I'm not disagreeing with any particular point in that link (not fully read because I'm using a phone now) this is where I got the notion from. Quote:
On Error GoTo 0 disables error handling in the current procedure.
I think that to disagree suggests that the M$ info is wrong or perhaps not universally understood. Perhaps I will remember to do some testing when this vacation is over.
 
Upvote 0
I think we are actually probably talking about "apples and oranges" here ("error handling" vs. "default error messaging").
If they currently have a specific error handling routine in place, yes that would bypass it as is written.

But to invoke an error handling routine in the first place, you would need a line like:
Rich (BB code):
On Error GoTo ErrorHandlingProcName
where ErrorHandlingProcName is the name of the procedure

So, if they have one already in place, they could simply just invoke that again like this:
VBA Code:
On Error Resume Next
Range("A2:A15000").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo [I]ErrorHandlingProcName[/I]
Or, alternatively, they could add just update their current Error Handling Procedure to handle the error code produced by this situation.

If they do not currently have any error handling procedure in there code, they can use what I gave them "as-is". It will work.
Here is a simple example that shows that Excel default error messages return when using "On Error GoTo 0".
VBA Code:
Sub MyTest()

    On Error Resume Next
    MsgBox "Test 1: " & 5 / 0
    On Error GoTo 0
   
    MsgBox "Test 2: " & 5 / 0
   
End Sub
You can see from the test, that the first test (the one between those two statements) will be ignored, while the second one will return the typical division by 0 error message.
 
Upvote 0

Forum statistics

Threads
1,215,391
Messages
6,124,673
Members
449,178
Latest member
Emilou

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