Macro for Lookup

macdca

Board Regular
Joined
Sep 28, 2010
Messages
170
Can anyone tell me how to write a macro for a VLOOK up procedure, lining to a worksheet which looks up over 2 worksheets?

Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
How does it link with the look u sheet? Do I have to have them both open at the same time? I hae pasted the sheet in and the formulas but cant get it to run. The example has range =VLOOKAllSheets("Dog",C1:E20,2,FALSE)
what wrksheet is this pointing to for the look up, who does it go from C to E20? My range is up to 30000 rows and the look up value is in column e of my datasheet and column A of the look up sheet??
 
Upvote 0
basically I am looking up values in column E of the active workbook against sheet 1, sheet 2 and sheet 3 in another look up workbook saved somewhere?
 
Upvote 0
OK, i have copied the sheets in and called them 1, 2 and 3
=VLOOKAllSheets("Post Code",E2:G6500,2,FALSE) is the formula I have entered and I am getting compile error?
E is the post code field/column on my main sheet, and I only have columns A to G?
 
Upvote 0
I am getting invalid outside procedure, and the line highlighted below is the one with the problem?


'Use VLOOKUP to Look across ALL Worksheets and stops at the first match found.
'''''''''''''''''''''''''''''''''''''''''''''''''
Dim wSheet As Worksheet
Dim vFound

On Error Resume Next

For Each wSheet In ActiveWorkbook.Worksheets
With wSheet
Set Tble_Array = .Range(Tble_Array.Address)
vFound = WorksheetFunction.VLookup _
(Look_Value, Tble_Array, _
Col_num, Range_look)
End With
If Not IsEmpty(vFound) Then Exit For
Next wSheet

Set Tble_Array = Nothing
VLOOKAllSheets = vFound
End Function
 
Upvote 0
You haven't got all the code there. It should be

Code:
Function VLOOKAllSheets(Look_Value As Variant, Tble_Array As Range, Col_num As Integer, Optional Range_look As Boolean)
''''''''''''''''''''''''''''''''''''''''''''''''
'Written by OzGrid.com
'Use VLOOKUP to Look across ALL Worksheets and stops at the first match found.
'''''''''''''''''''''''''''''''''''''''''''''''''
Dim wSheet As Worksheet
Dim vFound
On Error Resume Next
    For Each wSheet In ActiveWorkbook.Worksheets
        With wSheet
        Set Tble_Array = .Range(Tble_Array.Address)
            vFound = WorksheetFunction.VLookup _
            (Look_Value, Tble_Array, _
            Col_num, Range_look)
        End With
        If Not IsEmpty(vFound) Then Exit For
    Next wSheet
    Set Tble_Array = Nothing
    VLOOKAllSheets = vFound
End Function
 
Upvote 0
I have used this in the project now and used the formula =VLOOKAllSheets("Post Code",E2:G6500,2,FALSE)

I am getting 0 in each field. Im not clear whhat the E and G ranges are representing here? Also, I would like it to run as a macro rather than pasting in a formuls if thats possible?
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

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