Using VLOOKUP in VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using VBA code to print reports from an SQL database using Excel as the
front end. I am trying to use VLOOKUP to find data from a list in one
spreadsheet and include it in an Excel spreadsheet. Here is the code:

If
Application.WorksheetFunction.IsNA(Application.WorksheetFunction.VLookup(cnn,
Sheets("Custname").Range("CN"), 4, False)) = False Then
cname = Application.WorksheetFunction.VLookup(cnn, Sheets
"Custname").Range("CN"), 4, False)
Else
cname = "--"
End If

If the data cannot be found, I want to enter dashes in the variable
'cname'l, but if data is found, I want to enter that data in the variable.

The code runs fine as long as the lookup fines data, but if it doesn't the
program halts. What am I doing wrong, and if this doesn't work, what will?

Thanks.
 
Greogrory

Adjust this code its something I have used in the past.

Result = Application.VLookup(Find_CMR, GMB_Table, C + 1, 0)
If IsError(Result) Then
.Cells(5 + i + step, 12 + C) = "--"
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top