Repeat VBA for each column

Jonnny

New Member
Joined
Dec 19, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am using vlookup to return values in each column and I try to have a VBA set-up.
Currently with below VBA, I need to change "E" and 2 in each column.
e.g) I want to return values in column E and the column index in another sheet is 2. Next column, I change column "F" and the column index in another sheet is 3. Next, column "G" and column index 4.......
Is there a way to repeat it using VBA?

authorWs.Range("E" & x).Value = Application.WorksheetFunction.vlookup( _
authorWs.Range("A" & x).Value, dataRng, 2, False)

VBA Code:
Sub Vlookup_Entire_Sheet()

Dim authorWs As Worksheet, detailsWs As Worksheet
Dim authorsLastRow As Long, detailsLastRow As Long, x As Long
Dim dataRng As Range

Set authorWs = ThisWorkbook.Worksheets("Entire At once")
Set detailsWs = ThisWorkbook.Worksheets("Details")

authorsLastRow = authorWs.Range("A" & Rows.Count).End(xlUp).Row
detailsLastRow = detailsWs.Range("A" & Rows.Count).End(xlUp).Row

Set dataRng = detailsWs.Range("A3:CI" & detailsLastRow)

For x = 2 To authorsLastRow
On Error Resume Next
authorWs.Range("E" & x).Value = Application.WorksheetFunction.vlookup( _
authorWs.Range("A" & x).Value, dataRng, 2, False)
Next x

End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
try this changing this:

VBA Code:
For x = 2 To authorsLastRow
On Error Resume Next
authorWs.Range("E" & x).Value = Application.WorksheetFunction.vlookup( _
authorWs.Range("A" & x).Value, dataRng, 2, False)
Next x
to:
VBA Code:
For x = 2 To authorsLastRow
For y = 1 To 4 ' assuming repeat for 4 columns
On Error Resume Next
authorws.Range(Cells(x, 4 + y), Cells(x, 4 + y)).Value = Application.WorksheetFunction.VLookup( _
authorws.Range("A" & x).Value, dataRng, (1 + y), False)
Next y
Next x
 
Upvote 0
try this changing this:

VBA Code:
For x = 2 To authorsLastRow
On Error Resume Next
authorWs.Range("E" & x).Value = Application.WorksheetFunction.vlookup( _
authorWs.Range("A" & x).Value, dataRng, 2, False)
Next x
to:
VBA Code:
For x = 2 To authorsLastRow
For y = 1 To 4 ' assuming repeat for 4 columns
On Error Resume Next
authorws.Range(Cells(x, 4 + y), Cells(x, 4 + y)).Value = Application.WorksheetFunction.VLookup( _
authorws.Range("A" & x).Value, dataRng, (1 + y), False)
Next y
Next x
Thanks a lot. It is working.

Is it also possible to loop specific columns?
e.g) I want to return values in column E and the column index in another sheet is 2. Next column, I change column "G" and the column index in another sheet is 5. Next, column "I" and column index 15. Next column "Z", column Index 27, etc
 
Upvote 0
yes that is very easily done: since there is a "columns" loop already we just change the referencing to use two arrays to lookup the correct column numbers:
VBA Code:
' E , G , I ,Z
targetcols = Array(5, 7, 9, 26)
srccols = Array(2, 5, 15, 27)
For x = 2 To authorsLastRow
For y = 0 To 3 ' assuming repeat for 4 columns
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
 
Upvote 0
Thanks a lot. It is working well.
Would it be even possible to have this function only for filtered range? or even from not opened workbook from a certain folder?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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