VLookup not working in VBA

DonEB

Board Regular
Joined
Apr 26, 2016
Messages
128
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I'm attempting to do a VLookup in a macro while using the Match function to dynamically determine which column to look in.

I did some testing by sending values to a Msgbox to validate I am passing the correct values.
Ex: MsgBox "Match Value is : " & Match1 <== the value was 13
MsgBox "Lookup Value is : " & Sheet2.Range("A2").Value <== the value was 8

In both both of the above examples, the value that I expected to be passed were. However, the value I am expected to be "Found" using the VLoopup was not correct.

Match1 = WorksheetFunction.Match(Sheet2.Range("B2").Value, Range("N1:U1"), 0) + 3
FoundValue = Application.WorksheetFunction.VLookup(Sheet2.Range("A2").Value, Sheet2.Range("K3:U10"), Match1, False)

Note: Expected results is ==> 0.44444

I'm actually coming back with the number "0".

Section of Table Used to Lookup:
NOTES: (1) the real table does extend beyond to column U and down to row 10
(2) the ONLY difference is that I changed the names. All numerical values are the same.
KLMNO
1813
2DentBulls
33Doe.75.33333.33333
44Zully.6600
55Hill.75.33333.33333
67Smith.66.25.25
78Dent.01.44444
89Vails.8.25.25

<tbody>
</tbody>

Can anyone see what I'm doing wrong? Your help would be appreciated.

Thank you.
 
Did you read post #7 ? with a single line you can find the value.

Do you want all the A-B, A-C, A-D, B-C, B-D and C-D combinations, or only the A-B and C-D combinations?

You want the result of all the combinations in an arrangement or better yet, in Excel cells. Or you just want a result.
Why not better raise everything you have, what you want to do and what you want as a result.
The above is because you started with a simple search that was solved because you were not correctly declaring a variable, but now we are going in a loop of combinations, but you still do not ask what your final objective is.
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Your problem is you defined myarray as type long, msgbox only types out strings, so the assignment to myarray is perfectly ok ( I think) I think the problem is just with your method of looking at it.
try this:

Code:
dim tt as string


tt = cstr(myarray(X,y)
MsgBox ("This is my array value: " & tt)

or faster try looking at myarray on the locals window.
 
Last edited:
Upvote 0
I finally got back to my computer and solved your problem you need to change the declaration of myarray
to:
Code:
Dim MyArray(1 To 5, 1 To 5) As Variant
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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