Hello,
I am using vlookup to return values in each column and I try to have a VBA set-up.
Currently with below VBA, If I want to return values in column E and the column index in another sheet is 2, I just change the referencing to use two arrays to lookup the column numbers.
However, instead of using two arrays, is it possible I give the column numbers from certain cells in excel sheet?
e.g)
AS-IS Need return values in column E, F, G, Z (in targetcols array it is 5,7,9,26) in another sheet column index 2, 5, 15, 27.
TO-BE Cell A2 - mentioned 5, Cell A3 - mentioned 7, Cell A4-9, Cell A5-26 / In Cell B2 - mentioned 2, Cell B3 - mentioned 5, ....15....27.
Does VBA have such funtion?
I am using vlookup to return values in each column and I try to have a VBA set-up.
Currently with below VBA, If I want to return values in column E and the column index in another sheet is 2, I just change the referencing to use two arrays to lookup the column numbers.
However, instead of using two arrays, is it possible I give the column numbers from certain cells in excel sheet?
e.g)
AS-IS Need return values in column E, F, G, Z (in targetcols array it is 5,7,9,26) in another sheet column index 2, 5, 15, 27.
TO-BE Cell A2 - mentioned 5, Cell A3 - mentioned 7, Cell A4-9, Cell A5-26 / In Cell B2 - mentioned 2, Cell B3 - mentioned 5, ....15....27.
Does VBA have such funtion?
VBA Code:
Sub Vlookup_Entire_Author_Sheet()
Dim AuthorWs As Worksheet, DetailsWs As Worksheet
Dim AuthorLastRow As Long, DetailsLastRow As Long, x As Long
Dim dataRng As Range
Set AuthorWs = ThisWorkbook.Worksheets("Author")
Set DetailsWs = ThisWorkbook.Worksheets("Details")
AuthorLastRow = AuthorWs.Range("A" & Rows.Count).End(xlUp).Row
DetailsLastRow = DetailsWs.Range("A" & Rows.Count).End(xlUp).Row
Set dataRng = DetailsWs.Range("A2:DU" & DetailsLastRow)
' E , G, I, Z / 2,5,15, 27
targetcols = Array(5, 7, 9, 26)
srccols = Array(2, 5, 15, 27)
For x = 2 To AuthorLastRow
For y = 0 To 3
On Error Resume Next
AuthorWs.Range(Cells(x, targetcols(y)), Cells(x, targetcols(y))).Value = Application.WorksheetFunction.VLookup( _
AuthorWs.Range("A" & x).Value, dataRng, srccols(y), False)
Next y
Next x
End Sub