[英]VBA to Vlookup values across multiple rows and columns Excel
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
.我在想它可能正在使用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..上面的代码适用于一列,但我不知道如何在第 3、4、5 列等上循环和执行。
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).对于源范围 A6:E9(带有列标题)和目标范围 A1:E4(带有列标题)。 You can adjust col.column in the formula to match the desired col number in the source range您可以调整公式中的 col.column 以匹配源范围中所需的列号
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.