clear cells using VBA

khickey

New Member
Joined
Apr 16, 2002
Messages
16
I need to clear all cells in a column that have a particular value. The value is sometimes a constant like "0" (zero) and sometimes is a value based on a formula. Is there a VBA code that will allow me to select those cells in column matching a particular value and clear them?

Cheers
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This should work on formulae results or values:

Code:
Sub deltime()
For Each Cell In [a:a]
If Cell.Value = "0" Then Cell.ClearContents 'put any value you want here
Next Cell
End Sub
Change [a:a] to the appropriate column. Hope this helps.

_________________
Cheers,<font size=+2><font color="red"> Nate<font color="blue">O</font></font></font>
"Me no are no nice guy."
This message was edited by NateO on 2002-04-25 15:49
 
Upvote 0
This looks really helpful, when i try is though i get
Run Time Error 13:

"Type Mismatch"

and the below is highlighted

If Cell.Value = "00/01/1900" Then

I have tried changing the month and day around and also tried changing it to "ben" and the same error occurs.

Secondly, will this work on a VLOOKUP result, which 00/01/1900 is

Thanks

Ben

PS. I've tried this on some other columns and managed to get it mostly working, it seems the VLOOKUP result is scuppering things, any suggestions?
 
Last edited:
Upvote 0
that code won't work for a vlookup result. to do that you need to copy the vlookup result and paste it as a value. so if column A is your vlookup column, I'd copy and paste column A onto itself as values and then do a find/replace for specific values.
 
Upvote 0
Do you have any error values in your data? You will get a Type Mismatch error if you try and compare error values like that.
 
Upvote 0
Thanks i've tried that but still no luck, even changed from date to general, i think it may be as Rorya says, "00/01/1900" is the result i'm trying to lose, this i guess is an error as it is saying that the data in the other spreadsheet is blank.

Is there a way around this, a way to blank out the errors? and not simply changing the font to white

I'm wondering also if it may be the way date is formatted within the VBA, should it be done dd/mm/yyyy or is there another format required?
 
Last edited:
Upvote 0
00/01/1900 is not an error. If the VLOOKUP were returning an error, you would see #N/A in the cell. You could try using:
Code:
If Cell.Value2 = 0 then
 
Upvote 0
You could add IF logic to your VLOOKUP to evaluate if it returns #N/A or #VALUE! using the ISERROR function. It will require you to have the VLOOKUP in the cell formula more than once, so on large spreadsheets it might be a concern since the VLOOKUP is a volatile function.
 
Upvote 0

Forum statistics

Threads
1,213,504
Messages
6,114,020
Members
448,543
Latest member
MartinLarkin

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