Fast question: get replace blanks to exit gracefully

Imrhien

New Member
Joined
May 5, 2011
Messages
27
G'day guys!

Should be an easy one for you pros. Here is my current script:

Columns("R:R").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "FALSE"

Goal: replace blanks in column R with the text "FALSE".
Issue: This works fine except when there are no blanks in the column - the script crashes and the line has to be removed to continue execution.

So, all I need is a method to get this script to exit gracefully when there are no blanks to convert. Can anyone help? :)
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Code:
Sub temp()
On Error GoTo errHndl
Columns("R:R").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "FALSE"
errHndl:
End Sub


Use error handler.
 
Upvote 0
Or similarly,
Code:
Sub x()
    On Error Resume Next
    Columns("R").SpecialCells(xlCellTypeBlanks).Value = "FALSE"
End Sub
 
Upvote 0
Thanks folks, that works flawlessly. Rather pleased with the first method; reminds me of batch scripting with the GOTO =D

This is very beneficial to me. I'm expecting non-savvy users to be using this script, so crushing any script crashes is vital. My macro is complete!
 
Upvote 0
Thanks folks, that works flawlessly. Rather pleased with the first method; reminds me of batch scripting with the GOTO =D

This is very beneficial to me. I'm expecting non-savvy users to be using this script, so crushing any script crashes is vital. My macro is complete!
Actually, they're both essentially the same method.

Just that one has error handling terminating when the script terminates, the other it terminates at a specified point in the code.

And one selects the blanks/FALSE, the other doesn't. Selection is rarely needed in VBA code. Among other effects it can slow VBA codes considerably.

Also, if you've got a lot of blanks (>10k or so) then you're likely to have crashes using the special cells approach. Crashes from which the error handlers in the above codes will neither warn you nor save you.
 
Upvote 0

Forum statistics

Threads
1,224,502
Messages
6,179,126
Members
452,890
Latest member
Nikhil Ramesh

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