"Find" and #N/A - error when not found

DonAndress

Active Member
Joined
Sep 25, 2011
Messages
362
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello.

I am trying to check if in column there is a formula returning error #N/A.
I use the below line of code.
If #N/A is found, it works perfectly.
But it it's not found (there is no such error in column) I get 'Object variable or With block variable not set' and macro breaks. I don't have any 'With' block in the code.

VBA Code:
searchNA = destWkb.Sheets("temp").Range("H:H").Find("#N/A", LookIn:=xlValues, searchdirection:=xlPrevious, searchorder:=xlByRows).row
where destWkb = ThisWorkbook

May I ask for guidance how to fix it?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hello.

I am trying to check if in column there is a formula returning error #N/A.
I use the below line of code.
If #N/A is found, it works perfectly.
But it it's not found (there is no such error in column) I get 'Object variable or With block variable not set' and macro breaks. I don't have any 'With' block in the code.

VBA Code:
searchNA = destWkb.Sheets("temp").Range("H:H").Find("#N/A", LookIn:=xlValues, searchdirection:=xlPrevious, searchorder:=xlByRows).row
where destWkb = ThisWorkbook

May I ask for guidance how to fix it?
You really need to correct the formulas that produce the errors.

Can you post the formulas and a sample of your data using XL2BB.

Can you update your profile to let people know what version you are on.
 
Upvote 0
May I ask for guidance how to fix it?
What do you want to happen if "#N/A" is not found?

BTW I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Am no expert at VBA but this seems to work

Code:
Sub k1()
Set destWkb = Application.ThisWorkbook
Set searchNA = destWkb.Sheets("Sheet1").Range("A:A").Find("#N/A", LookIn:=xlValues, searchdirection:=xlPrevious, searchorder:=xlByRows)
If Not searchNA Is Nothing Then
    MsgBox searchNA.Row
End If
End Sub

Using .row at the end of the search statement caused the error so I placed it on the MsgBox statement
 
Upvote 0
Solution
You really need to correct the formulas that produce the errors.

Can you post the formulas and a sample of your data using XL2BB.

Can you update your profile to let people know what version you are on.
Well yes, I need to correct my formulas.
But there's nothing really to post.
I have a basic column with vlookups, some of them return #N/A


What do you want to happen if "#N/A" is not found?

BTW I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
Well originally this piece of code served me to find and store row of first not empty cell. I wanted to use it to find #N/A and then have a simple IF statement.
BTW, account details updated.


Am no expert at VBA but this seems to work

Code:
Sub k1()
Set destWkb = Application.ThisWorkbook
Set searchNA = destWkb.Sheets("Sheet1").Range("A:A").Find("#N/A", LookIn:=xlValues, searchdirection:=xlPrevious, searchorder:=xlByRows)
If Not searchNA Is Nothing Then
    MsgBox searchNA.Row
End If
End Sub

Using .row at the end of the search statement caused the error so I placed it on the MsgBox statement
So, this works, thank you.

Although I don't understand the mechanism.
Why this gives me row number stored in a variable
VBA Code:
searchNA = destWkb.Sheets("Sheet1").Range("A:A").Find("*", LookIn:=xlValues, searchdirection:=xlPrevious, searchorder:=xlByRows).Row

And why this does not store Empty/Null when there is no #N/A?
VBA Code:
searchNA = destWkb.Sheets("Sheet1").Range("A:A").Find("#N/A", LookIn:=xlValues, searchdirection:=xlPrevious, searchorder:=xlByRows).Row
 
Upvote 0
From Microsoft help: Range.Find method (Excel)
• Range.Find returns "A Range object that represents the first cell where that information is found."
• "This method returns Nothing if no match is found"

".Row" is a property of the Range object, so when it doesn't find anything and the Range Object is nothing trying to get the ".Row" property errors out.
(Object Variable is not set)
 
Upvote 0
Upvote 0
Your existing worksheet formula, which sometimes returns a #N/A error.
=VLOOKUP(D1,G1:K100,2,0)

What is the simple IF statement?
if searchNA is nothing then
[some code here]
end if

but this was not working when I still had .Row at the end:
searchNA = destWkb.Sheets("Sheet1").Range("A:A").Find("#N/A", LookIn:=xlValues, searchdirection:=xlPrevious, searchorder:=xlByRows).Row
 
Upvote 0

Forum statistics

Threads
1,215,187
Messages
6,123,540
Members
449,107
Latest member
caya

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