Add IF statement to Range

jonathann3891

Board Regular
Joined
Apr 27, 2015
Messages
109
Is it possible to add an IF statement to this code?

Code:
Private Sub CommandButton2_Click()
Dim LR As Long
LR = Range("C" & Rows.Count).End(xlUp).Row
Range("C10:C" & LR).SpecialCells(xlCellTypeConstants, 23).Clear
End Sub

It works perfect, but if the cells are blank it give me the following error:
Run-time error '1004':
No cells were found

I dont want it go give an error if the cell is blank!

Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You can add an error handling statement:

Code:
Private Sub CommandButton2_Click()
Dim LR As Long
On Error GoTo 500
LR = Range("C" & Rows.Count).End(xlUp).Row
Range("C10:C" & LR).SpecialCells(xlCellTypeConstants, 23).Clear
500
End Sub
 
Upvote 0
This line:

Code:
On Error GoTo 500

basically says if the code errors, then go to the line designated as 500. In other words, it skips the code between the GoTo and 500 statements.
 
Upvote 0
I'm having a small problem. If I clear the data using this macro, and then it's accidently activated again it will clear everything on Column C.

I thought stating the range
Code:
Range("C10:C" & LR).
would stop the code from going beond the range I specified?
 
Upvote 0
If your variable is set to the count of column C, it will clear everything from C10 down. Is that not what you want to happen?
 
Upvote 0
Thats what I want to happen. But if that range is empty and the macro is activated it will clear something that is on C5, etc.
 
Upvote 0
I see. Try this:

Code:
Private Sub CommandButton2_Click()
If Range("C" & Rows.Count).End(xlUp).Row < 10 Then Exit Sub
Range("C10:C" & Range("C" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeConstants, 23).Clear
End Sub

You don't need to use a variable or error handler with this.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,665
Members
449,045
Latest member
Marcus05

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