vba for vlookup to return multiple columns

ThomasOES

Board Regular
Joined
Aug 29, 2017
Messages
174
Hello,
I would like to use VLookup to match the name entered in a userform dialog box to a name in Sheet("CRM_Lib") Column A. Then return the data that is ~20 columns wide from Sheet("CRM_Lib") to Sheet("Header") Cell ("Y42").

For instance the userform asks for a part #. I enter it, then the data for the part (contained in Sheet "CRM_Lib") is placed is Sheet "Header" , Starting at cell "Y42".

Heres an example of what I'm trying

The top row is chemical elements
The next row is the % of a part #.
I currently use a userform to enter a part # in the same row 3 columns to the left and then pastes a vloolup formula across the cells, but I'd like vba to do it with code.

AlAsB!BeCCa!CoCrCuFeLaMg!MnMoN!NbNiPPbSSbSiSnTaTiVWZnZr
(<0.002)-1-1-11.06-10.04716.870.09-1-1-11.150.54060.0050.350.022-10.007-10.47(0.004)-10.0010.130.11-1-1

<tbody>
</tbody>

Currently I try

Code:
Range("Y42") = Application.WorksheetFunction.VLookup _
(Range("V42").Value, Sheets("CRM_Lib").Range("A2:AD2001"), 2, False)

The userform puts the part # in "V42". But I only get the data from the second column. In this case (<0.002). (The part # is the column 1). How to get the whole row?

Thanks

Tom
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
hello ThomasOES.

Please paste the following code in the code page of the Header sheet. To get there right-click the Header sheet's label and select View code.
Code:
Sub Worksheet_Change(ByVal Target As Range)
'paste this sub in the code page of the Header sheet
    Dim lastColumn As Long, lastLabel As Long
    Dim matchedRow As Long, lastRow As Long
    
    If Target.Address(0, 0) = "V42" Then 'only act if cell V42 changes
        With ThisWorkbook.Worksheets("CRM_lib")
            lastRow = .Range("A2").End(xlDown).Row
            matchedRow = 3
            While .Cells(matchedRow, 1).Value <> Target.Value _
              And matchedRow <= lastRow
                matchedRow = matchedRow + 1
            Wend
            Range(Range("Y42"), Range("Z42")).Clear
            If matchedRow <= lastRow Then
                lastColumn = .Cells(matchedRow, 1000).End(xlToLeft).Column
                Range(Range("Y42"), Cells(42, Columns("Y").Column + lastColumn)).Value = _
                .Range(.Cells(matchedRow, 2), .Cells(matchedRow, lastColumn)).Value
            End If
        End With
    End If
End Sub

This code runs every time something on the sheet changes. Since it starts with checking if the change occurred in cell V42, only changes in that cell's value will trigger the lookup like action.
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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