Can anyone correct my 3liner of codes?

edlim85

Board Regular
Joined
May 4, 2009
Messages
178
im getting a error here..pls help:eeek:

As the code, can tell what i want to do is go through each cells in my used range. clearcontent for cells containing "NA" or "#N/A"

For Each cell In w1.UsedRange.cells
If cell.Value = "NA" Or cell.Value = "#N/A" Then cell.ClearContents
Next cell


Regards
Edmund
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Thanks RichardMGreen for ur prompt reply.

dim cell as range, i step into my macro the cells until #N/A which is error 2042. how do i clear this?
 
Upvote 0
I have NA (which is my input) and #N/A that i want to clear.

i just found this. maybe i should write separate code to clear NA and #N/A

For Each cell In Range("MSLnew").SpecialCells(xlCellypeFormulas,xlErrors)
If cell.Text = "#N/A" Then cell.ClearContents
Next cell
 
Upvote 0
Change this line:-
If cell.Value = "NA" Or cell.Value = "#N/A" Then cell.ClearContents
to
If cell.Value = "NA" Or iserror(cell.Value) Then cell.ClearContents

You can't test for an error in the way you're trying to. This way you'll get rid of all errors, not just the #N/A ones.
 
Upvote 0
What have you defined cell as?

This bit
w1.UsedRange.cells
should just be
w1.UsedRange
Actually, including the .Cells is probably better since it specifically states what property of the range to use, rather than relying on the default as many of us often do. It certainly isn't wrong to include it.

For example, take a fresh worksheet and fill a small (say 3 x 3) range somewhere on the sheet with data.

Put the below code in a Module and step through a line at a time with F8 and observe the number of loops and the sheet results.

Then clear the cell colour and change the .Rows to .Cells and repeat the step-through.
Code:
Sub test()
    Dim cell As Range
    
    For Each cell In ActiveSheet.UsedRange.Rows
        cell.Interior.ColorIndex = 3
    Next cell
End Sub
 
Upvote 0
If cell contains an error, a typemismatch error results from trying to compare cell.Value = "NA". This protects against that

Code:
If CStr(cell.Value) = "NA" Or IsError(cell.Value) Then cell.ClearContents

This will test for the string "NA" and the error value "#N/A"
Code:
If CStr(cell.Value) = "NA" Or CStr(cell.Value) = CStr(CVErr(xlErrNA)) Then cell.ClearContents
 
Upvote 0
Thanks to all for your replies. I'm still new and learning here.
:)

If CStr(cell.Value) = "NA" Or CStr(cell.Value) = CStr(CVErr(xlErrNA)) Then cell.ClearContents

this works.

thanks again


Regards
Edmund
 
Upvote 0
Peter - I must admit I've never used .Cells when I've referenced UsedRange, but I'm happy to be corrected.
 
Upvote 0
Edmund

Stepping through every cell in the used range seems like it could be more work than is needed. How big is your used range?

Do you have other error values besides #N/A that you want to keep, or could youn use something like this to get them all in a couple of swipes?
Code:
With Sheets("MSLnew").UsedRange
    On Error Resume Next
    .SpecialCells(xlCellTypeFormulas, xlErrors).ClearContents
    On Error GoTo 0
    .Replace What:="NA", Replacement:="", LookAt:=xlWhole, _
        Searchformat:=False, ReplaceFormat:=False
End With
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

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