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, 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
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I'm not sure, if it is possible, because your array values don't seem to follow a pattern.
e.g. if your Array was like A={1,3,5,7,9} and B={2,4,6,8,10} and your counter variable (lets say) i changes steps of 1 starting at 0, then the pattern to replace the array could be:
A(i) = 2*i +1 or B(i) = 1 + 2*i + 1

But your data - targetcols = {5, 7, 9, 26} and srccols = {2, 5, 15, 27} - doesn't seem to have anything in common.
At least nothing I can see...

VBA offers many functions, but they all follow some logic or rules. If you can tell me the logic of your data or a rule it follows, maybe something will work.
 
Upvote 0
Author Tab
ABCDEFGHIJK
Row1 age  weight  height cost
Row2          

Details Tab
ABCDEFGHIJK
Row1age weight   height  cost
Row2
35​
 
70​
   
170​
  
50​

Unfortunately, there is no specific pattern. The order of columns in Author tab always changes. So far, I modified manually the column number in Author tab.
But I am wondering if it is possible instead of modifying the column number every time, I put the column number in cells and VBA follow the column number from the cells.


Aligning Tab
ABCDEFGHIJK
Row1
3​
 
6​
   
9​
  
11​
Row2age weight   height  cost
Row3
35​
 
70​
   
170​
  
50​

As above, I have entered 3, 6, 9, 11 in the aligning tab and is it possible VBA repeat the value in Author tab?


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)

' C, F, I, K / 2,4,8,11
targetcols = Array(3, 6, 9, 11)
srccols = Array(2, 4, 8, 11)
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
 
Upvote 0
Is there any reason you can't use match using the headings to work out the column number ?
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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