looping vba vlookup column index number

tomwax46

New Member
Joined
May 22, 2022
Messages
1
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
hey,hope you are doing great, the below code matches the first column between 2 workbooks then copy & paste the matched results using vlookup function, the problem is that vlookup only shows 1 cell result while i need the whole row to be copied so i tried to loop the process to chnage column index number step by step but it doesn't seem to work. any help or tip would be really appreciated.

VBA Code:
Sub solution()

Dim oldRow As Integer
Dim newRow As Integer
Dim lrow_output As Integer
Dim WB_Input As Workbook
Dim WB_Output As Workbook
Dim WS_Input As Worksheet
Dim WS_Output As Worksheet
Dim funcStr As String
Dim i As Integer

Set WB_Input = Workbooks("File.xlsm")
Set WB_Output = Workbooks("output1.xlsx")

Set WS_Input = WB_Input.Worksheets("input")
Set WS_Output = WB_Output.Worksheets("Sheet1")

With WS_Output
    lrow_output = .Cells(.Rows.Count, 1).End(xlUp).Row
End With

With WS_Input
For i = 2 To 3 Step 1
    funcStr = "=IFERROR(VLOOKUP(" & Cells(1, 1).Address(False, False) & "," & "'[" & WB_Input.Name & "]" & .Name & "'!" & Range(.Columns(1), .Columns("B:Z")).Address & ",i,0),"""")"

With WS_Output
    .Cells(1, i).Formula = funcStr
    .Cells(1, i).Copy
    Range(.Cells(1, i), .Cells(lrow_output, i)).PasteSpecial xlPasteFormulas
    WS_Output.Calculate
    Range(.Cells(1, i), .Cells(lrow_output, i)).Copy
    Range(.Cells(1, i), .Cells(lrow_output, i)).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    End With
    
Next i
End With
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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