Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Small vlookup issue

Posted on 2011-05-02
Medium Priority
293 Views
Hello all,

I have a small problem with a VLookup macro.

I want to put the result for that formula in column N of active cell.
=VLookup(S2135,'Rate Group'!K:L,2,0)

But it leaves the cell empty all the time.

Here is the macro in my

``````Private Sub Worksheet_Change(ByVal Target As Range)

Dim QW As Range
Set QW = Intersect(Target, Range("S6:S" & Rows.Count))
If Not QW Is Nothing Then
If QW.Value > "" Then
QW.Offset(, -5).Formula = "=IF(ISNA(VLOOKUP(QW.Value,'Rate Group'!K:L,1,0))"

End If
QW.Offset(, -5).Value = ""
End If
``````

Thanks

0
Question by:Wilder1626

LVL 15

Expert Comment

ID: 35504540
Private Sub Worksheet_Change(ByVal Target As Range)

Dim QW As Range
Set QW = Intersect(Target, Range("S6:S" & Rows.Count))
If Not QW Is Nothing Then
If QW.Value > "" Then
QW.Offset(, -5).Formula = "=IF(ISNA(VLOOKUP(QW.Value,'Rate Group'!K:L,1,0))"

End If
'WHAT HAPPENS IF YOU REMOVE THIS LINE?  QW.Offset(, -5).Value = ""
End If
cheers
cslarsen
0

LVL 11

Author Comment

ID: 35504650
Hello

I have an error in the formula.

Here is the excel spreadsheet has an example.

Thanks again.
DC-Province.xlsm
0

LVL 93

Accepted Solution

Patrick Matthews earned 1600 total points
ID: 35504939
Assuming you want to return the name of the province if found, N/A if there is no entry for the code supplied, or a blank if the code column is blank...

``````Private Sub Worksheet_Change(ByVal Target As Range)

Dim QW As Range, cel As Range

Set QW = Intersect(Target, Range("S6:S" & Rows.Count), Me.UsedRange)
If Not QW Is Nothing Then
For Each cel In QW.Cells
If cel <> "" Then
cel.Offset(, -5).Formula = "=IFERROR(VLOOKUP(""" & cel & """,'Rate Group'!K:L,2,0),""N/A"")"
Else
cel.Offset(, -5).ClearContents
End If
Next
End If

End Sub
``````
0

LVL 19

Assisted Solution

Arno Koster earned 400 total points
ID: 35504964
There are a number of issues :

- 3 opening brackets '(' are used, while only 2 closing brackets ')' are in the formula.
the structure should be like
IF(ISNA(VLOOKUP([arguments])), 'then' value, 'else' value)

- you are referring to QW.Value in the formula, which exists only in VBA code, not in excel.
There are 2 choices : using the value of the changed cell in the formula : [...]Vlookup(" & QW.Value & "[...]
or using the cell itself : [...]vlookup(" & QW.address & "[...]

- the vlookup function requires a third argument being the column of the data set which is to be returned for a match (eg. 1 for K or 2 for L)
the structure should be like
VLOOKUP(value, dataset, resulting_column)

0

LVL 11

Author Comment

ID: 35505476
Ok, let me test this and i will let you all know the result.

Thanks again
0

LVL 11

Author Closing Comment

ID: 35511974

It works perfectly.

Have a great day
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are â€¦
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to usâ€¦
###### Suggested Courses
Course of the Month15 days, 17 hours left to enroll