Reputation: 281
I can not get the VLookup
function to work when using a named range. I am sure it has something to do with how I am referencing "COA_Range"
but cant find a solution that works
I have tried [], ([]), (""), [""],([""])......
(Below is an updated and expanded section of the code)
If Transaction_Type = 1 Then
Debug.Print "Transaction Type :"; Transaction_Type
Range("n10").Value = "Income"
Debug.Print "COA # = "; TransactionInfo.Income_COA_Number.Value
COA_Number = TransactionInfo.Income_COA_Number.Value
Debug.Print COA_Number
Range("n12").Value = TransactionInfo.Income_COA_Number.Value
'thought from STACK OVERFLOW
Debug.Print Range("COA_Range").Address()
COA_1 = Application.WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 2, False)
Debug.Print COA_1
Range("n13").Value = COA_1
Upvotes: 1
Views: 809
Reputation: 33672
Following @Jeeped comment, make sure that the value in your User_Form named "TransactionInfo" in the TextBox "Income_COA_Number" has a Numeric value, and so all the values in your Range("COA_Range")
cells.
I've added 2 optional solutions (pick the one you prefer):
Application.Match
.Find
.Code
Option Explicit
Sub VLookUpNamedRange()
Dim ws As Worksheet
Dim Transaction_Type As Long
Dim MyCOARng As Range
Dim COA_1 As Variant
Dim COA_Number As Long
Dim FndRng As Range
Set ws = Worksheets("Sheet7") '<-- modify "Sheet7" to your sheet's name
Set MyCOARng = ws.Range("COA_Range") '<-- set Range to "COA_Range" Named Range
COA_Number = TransactionInfo.Income_COA_Number.Value
' === Option 1: Use Application.Match ===
If Not IsError(Application.VLookup(COA_Number, MyCOARng, 2, False)) Then ' <-- VLookup Successful
COA_1 = Application.VLookup(COA_Number, MyCOARng, 2, False)
Else ' <-- VLookup failed
COA_1 = COA_Number & " Not found in 'COA_Range' "
End If
' === Option 2: Use Find ===
Set FndRng = MyCOARng.Find(What:=COA_Number, LookIn:=xlValues, lookat:=xlWhole)
If Not FndRng Is Nothing Then '<-- successful find
COA_1 = FndRng.Offset(, 2).Value
Else '<-- not found in your range
COA_1 = COA_Number & " Not found in 'COA_Range' "
End If
Debug.Print COA_1 ' <-- for DEBUG Only
End Sub
Upvotes: 1
Reputation: 281
Special thanks to @jainashish, @Shai Rado for the thoughtful responses. I was able to pick up a few pointers from each.
It was @Jeeped however who actually solved my problem. The "number" was being read as text and the CLng() expression worked for me. I have added my updated code below.
If Transaction_Type = 1 Then
Debug.Print "Transaction Type :"; Transaction_Type
Range("n10").Value = "Income"
'thought from STACKOVERFLOW
'need to make sure that the number taken fromt the userform is ACTUALLY a number and not text that looks like a number
'use CLng to convert
Debug.Print "COA # = "; CLng(TransactionInfo.Income_COA_Number.Value)
COA_Number = CLng(TransactionInfo.Income_COA_Number.Value)
Debug.Print "COA # = "; COA_Number
Range("n12").Value = COA_Number
'thought from STACK OVERFLOW
Debug.Print Range("COA_Range").Address()
'Yes the range is being found...
Dim COA_Range As Range
Set COA_Range = Range("COA_Range")
Debug.Print COA_Range.Address()
COA_1 = WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 2, False)
Debug.Print COA_1
Range("n13").Value = COA_1
COA_2 = WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 3, False)
Debug.Print COA_2
Range("n14").Value = COA_2
COA_3 = WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 4, False)
Debug.Print COA_3
Range("n15").Value = COA_3
COA_4 = WorksheetFunction.VLookup(COA_Number, Range("COA_Range"), 5, False)
Debug.Print COA_4
Range("n16").Value = COA_4enter code here
Upvotes: 0
Reputation: 5183
Try to use Application.Vlookup instead of using Application.WorksheetFunction.Vlookup. Then set a Variant equal to this and if no match is found then it would return Error 2042 which can be tested using IsError
See the Example Code below:
Dim ws As Worksheet: Set ws = ActiveSheet 'Change the sheet reference appropriately
Dim rngLookUpTable As Range: Set rngLookUpTable = ws.Range("COA_Range")
Dim vReturn As Variant
If Transaction_Type = 1 Then
Range("N10").Value = "Income"
COA_Number = TransactionInfo.Income_COA_Number.Value
Range("N12").Value = TransactionInfo.Income_COA_Number.Value
vReturn = Application.VLookup(COA_Number,rngLookUpTable, 2, False)
Range("N13").Value = IIF(IsError(vReturn),"Not Found",vReturn)
End If
WorksheetFunction version of VLookup and Match need error handling that re-routes your code to an error handler, returns to the next statement to evaluate, etc. With the Application functions, you can avoid that mess.
Upvotes: 0