简体   繁体   English

具有匹配功能的VBA Vlookup

[英]VBA Vlookup with Match function

I have created the pivot table using VBA code and named it as Pivot1. 我已经使用VBA代码创建了数据透视表,并将其命名为Pivot1。 All I'm looking is to set up a dynamic VBA Vlookup code as the row headers in the pivot may keep changing. 我要寻找的是设置动态VBA Vlookup代码,因为数据透视表中的行标题可能会不断变化。 I have used match function formula and I'm able to get the result when I apply this on the cell but unable to write a VBA code using this. 我使用了匹配函数公式,当将其应用于单元格但无法使用此函数编写VBA代码时,便可以获得结果。

=IFERROR(VLOOKUP($D18,Pivot1!$A$4:$D$27,MATCH(E$17,Pivot1!$A$4:$E$4,0),0),0)

Please help me in adding this match function in this VBA code - 请帮助我在此VBA代码中添加此匹配功能-

Range("E18").Activate

ActiveCell.Value = Application.WorksheetFunction.VLookup(ActiveCell.Offset(0, -1), Sheets("Pivot1").Range("A1:E50"), 2, 0)

ActiveCell.Offset(1).Select

If you're stuck using Application.Match(), though GetPivotData from Mat's Mug's suggestion would seem more appropriate, then you would add that similar to your Application.VLookUp()... i would recommend using a with statement, so you don't have to keep retyping the same thing: 如果您坚持使用Application.Match(),虽然Mat's Mug的建议中的GetPivotData似乎更合适,那么您将添加类似于Application.VLookUp()...我建议使用with语句,所以不要不必再重复输入相同的内容:

With Sheets("Pivot1") 
    ActiveCell.Value = Application.VLookup(ActiveCell.Offset(0, -1).Value, .Range("A4:D27"), Application.Match(ActiveCell.Offset(-1,0).Value, .Range("A4:E4"),0), 0)
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM