Code not finding criteria = "N/A"

kc_native

Board Regular
Joined
Jan 20, 2009
Messages
176
I am trying to execute a For Loop that goes through a range looking for cells that are blank, or contain an #N/A. The reason I have to do this is that I am wanting to do a CountA to find the number of cells that contain the data I want to count, but unless I do a ClearContents on the blank cells, the CountA counts them (and of course the #N/A cells as well). That one is a mystery to me, but here is the For Loop I am using to accomplish the ClearContents function:

Code:
For lngRow = 3 To Cells(Rows.Count, 1).End(xlUp).Row - 2
        If (Cells(lngRow, lngCol + 2) = "" Or Cells(lngRow, lngCol + 2) = "#N/A") Then
            Cells(lngRow, lngCol + 2).ClearContents
        End If
Next lngRow

The problem is that the For Loop is blowing up when it encounters the first #N/A, and giving me a Type Mismatch error. What am I doing wrong here?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Perhaps

Code:
For lngRow = 3 To Cells(Rows.Count, 1).End(xlUp).Row - 2
    If IsError(Cells(lngRow, lngCol + 2)) Then
        Cells(lngRow, lngCol + 2).ClearContents
    ElseIf Cells(lngRow, lngCol + 2) = "" Then
        Cells(lngRow, lngCol + 2).ClearContents
    End If
Next lngRow
 
Upvote 0
you are treating "N/A" as a cell value. It's not really an explicit value, it's more of a "state" that the cell is in. use the ISERROR() function instead.

If (Cells(lngRow, lngCol + 2) = "" Or ISERROR(Cells(lngRow, lngCol + 2)) Then

Sometimes, can't remember if it's more Excel or VBA, you will get an error if you try to test an N/A cell for a specific value. In that case, swap the order of your tests:

If ISERROR(Cells(lngRow, lngCol + 2)) OR (Cells(lngRow, lngCol + 2) = "" Then
 
Upvote 0
untested, though the below might work.

you should look at the COUNTIF and ISNA functions as a possible formula workaround as well.

Code:
For lngRow = 3 To Cells(Rows.Count, 1).End(xlUp).Row - 2
        If (Cells(lngRow, lngCol + 2) = "" Or application.worksheetfunction.isna(Cells(lngRow, lngCol + 2)) = true) Then
            Cells(lngRow, lngCol + 2).ClearContents
        End If
Next lngRow
 
Upvote 0
you are treating "N/A" as a cell value. It's not really an explicit value, it's more of a "state" that the cell is in. use the ISERROR() function instead.

If (Cells(lngRow, lngCol + 2) = "" Or ISERROR(Cells(lngRow, lngCol + 2)) Then

Sometimes, can't remember if it's more Excel or VBA, you will get an error if you try to test an N/A cell for a specific value. In that case, swap the order of your tests:

If ISERROR(Cells(lngRow, lngCol + 2)) OR (Cells(lngRow, lngCol + 2) = "" Then
Thanks, Chris. Your first line of code worked just great.

Now, can you tell me how a cell that appears blank in the cell location, and in the formula window, can be counted by a CountA function? I have never encountered this before.
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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