VBA Lookup array

mattless1

Board Regular
Joined
Apr 1, 2011
Messages
102
Hi All,

im looking for a little help if possible.

i have a code below which, looks for the range in column A Sheet2 and then looks for a range in sheet1 main data and returns a specified column from sheet1. this works fine, but i'd like it to look for the range in another workbook. and return a column from the other workbook.. an alternative to Xlookup.

this works fine on a single workbook.

VBA Code:
Sub macroLookup()

    ' Disable these Excel properties whilst macro runs
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
   
        ' Declare object variables for macro
        Dim lastRowIn, lastRowOut, i As Long
        Dim lookFor, j, inArray, outArray, findArray As Variant
        Dim inWks, outWks As Worksheet
       
            ' Set certain object variables
            Set inWks = ThisWorkbook.Sheets(1)  ' source input sheet with data
            Set outWks = ThisWorkbook.Sheets(2)  ' output sheet with specific values to find
           
                ' Find the last rows in the source & output fields of company names
                lastRowIn = inWks.Cells(Rows.Count, "A").End(xlUp).Row
                lastRowOut = outWks.Cells(Rows.Count, "D").End(xlUp).Row
               
                    ' Record the input array of data from A1
                    inArray = Range(inWks.Cells(1, 1), inWks.Cells(lastRowIn, 16))
                   
                    ' Load the find array from the output column in col D in Sheet2
                    findArray = Range(outWks.Cells(1, 4), outWks.Cells(lastRowOut, 4))
                   
                    ' Define the output array in col E in Sheet2
                    outArray = Range(outWks.Cells(1, 5), outWks.Cells(lastRowOut, 5))
                   
                        ' Error handler
                        On Error Resume Next
                       
                        ' First loop through the output names in col D, sheet2
                        For i = 2 To lastRowOut
                       
                            ' The second loop through the input names in col A, sheet1
                            For j = 2 To lastRowIn
                           
                                lookFor = findArray(i, 1)
                               
                                If inArray(j, 1) = lookFor Then
                                    outArray(i, 1) = inArray(j, 16)
                                    Exit For
                                End If
                               
                            ' move to next value in col A, sheet1
                            Next j
                   
                        ' move to next value in col D, sheet2
                        Next i
                       
            ' write out the output array values in col E, sheet2
            Range(outWks.Cells(1, 2), outWks.Cells(lastRowOut, 2)) = outArray
           
    ' Re-enable Excel properties as macro is complete & finished running
    With Application
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You can get a range from another Workbook with the Application with the following
VBA Code:
Dim wb as WorkBook
wb = Application.Workbooks.Open("C:\Users\User\Folder\File.ext")
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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