VBA Clear Content based on certain value

SophieF114

New Member
Joined
Aug 14, 2019
Messages
7
hi all

I have a column with a formula and want VBA to clear those cells taht contain the #Value ! error.. I put together below code, which doesn't error but also doesn't do what I want it to do?

Rich (BB code):
    Dim find As String
find = "#Value !"
Select Case find
Case Cells(2, 3).Value
Range("C2:C9999").Select
Selection.ClearContents
End Select
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
How about
Code:
Sub sophieF114()
   Range("C:C").SpecialCells(xlFormulas, xlErrors).ClearContents
End Sub
This will clear all cells in column C that have a formula returning an error.
 
Upvote 0
That is because your code is looking for a literal text string of "#Value" and not the "#Value" error.
You can use the ISERROR function to locate errors. See: https://www.techonthenet.com/excel/formulas/iserror.php

Also, you should NOT used reserved words like "find" as variables in VBA. It can cause ambiguity and errors, as VBA might not be able to determine is you are trying to reference the "find" variable or the "find" method (https://docs.microsoft.com/en-us/office/vba/api/excel.range.find). Better to pick names that aren't reserved words. Many people add the prefix "my" to the front, i.e. "myfind".
 
Upvote 0
Hi Joe

thank you for your reply and the added details about reserved words... makes complete sense and I will save this resource to make sure I don't use reserved words

Sophie
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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