I have a worksheet like such:
Name Num1 Num2 Num3 Num4
test1
test2
test3
test3 7% 6% 6% 6%
test1 4% 5% 4% 5%
test2 6% 6% 5% 4%
I am trying to create a macro button that copies the values from the below part of the worksheet to the above empty part based on the name column value.
I was thinking it could be using a vlookup
.
The values in the below part are in formula form so they would need to be pasted special as values with number formatting.
The result is as follows:
Name Num1 Num2 Num3 Num4
test1 4% 5% 4% 5%
test2 6% 6% 5% 4%
test3 7% 6% 6% 6%
test3 7% 6% 6% 6%
test1 4% 5% 4% 5%
test2 6% 6% 5% 4%
This is what I have tried so far:
Sub COPY()
With Range("B2:B4")
.Formula = "=VLOOKUP(A2,$A$8:$M$10,2,0)"
.Value = .Value
End With
End Sub
The above code works for one column, but I can't figure out how to loop and do it over column 3, 4, 5 etc..
Result from above code:
Name Num1 Num2 Num3 Num4
test1 4%
test2 6%
test3 7%
test3 7% 6% 6% 6%
test1 4% 5% 4% 5%
test2 6% 6% 5% 4%
Thanks for the help. Let me know if you have any questions.
for source range A6:E9 (with col headings) and target range A1:E4 (with col headings). You can adjust col.column in the formula to match the desired col number in the source range
Sub vlkup()
Dim col As Range
For Each col In Range("B2:E4").Columns
With col
.Formula = "=VLOOKUP($A2,$A$6:$E$9," & col.Column & ",False)"
.Value = .Value ' to convert formula to values
End With
Next
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.