Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for #N/A in Excel cell from VBA code

Tags:

excel

vba

I'm iterating through a range of cells which hold numbers with 2 decimal places. I need to check if the cell holds '#N/A', and if it does, I need to skip it. The problem is, when a cell holds a valid number, my if condition below throws a 'Type mismatch error'. How can I avoid this?

If (ActiveWorkbook.Sheets("Publish").Range("G4").offset(offsetCount, 0).Value <> CVErr(xlErrNA)) Then
'do something
End If
like image 771
xbonez Avatar asked Feb 28 '11 14:02

xbonez


People also ask

What does checking for you mean?

'Checkin someone out' definitely has meaning (as in, looking at or sizing someone up as as a potential love/attraction interest, or just examining up and down at how someone looks if they are attractive), but her saying "checkin' for" here makes me think it means something else.

What does check up on someone mean?

/tʃek/ to find out what someone is doing in order to make certain that the person is behaving correctly or legally: Dad is always checking up on me to make sure I'm doing my homework.

Is it checking in or checking in?

Check in, without a hyphen, is an action verb and denotes your action of checking in somewhere or, checking in on something. Check-in, with a hyphen, refers to either a place or time where you do your checking in and can be used either as a noun or adjective.


1 Answers

First check for an error (N/A value) and then try the comparisation against cvErr(). You are comparing two different things, a value and an error. This may work, but not always. Simply casting the expression to an error may result in similar problems because it is not a real error only the value of an error which depends on the expression.

If IsError(ActiveWorkbook.Sheets("Publish").Range("G4").offset(offsetCount, 0).Value) Then
  If (ActiveWorkbook.Sheets("Publish").Range("G4").offset(offsetCount, 0).Value <> CVErr(xlErrNA)) Then
    'do something
  End If
End If
like image 112
user637663 Avatar answered Sep 23 '22 16:09

user637663