VBA Excel:如何在 VBA 中使用 VLookup 和 IF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20934827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Excel: How can I use VLookup and IF in VBA?
提问by user3133588
How can I do this in VBA?
我怎样才能在 VBA 中做到这一点?
IF(VLOOKUP(E3;Tabel5;2;0)>=F26;VLOOKUP(E3;Tabel5;4;0)*F26;)
Thank you very much!
非常感谢!
回答by Dmitry Pavliv
try to use this code:
First way (you can use user defined function):
尝试使用此代码:
第一种方式(您可以使用用户定义的函数):
Function getSomeData(E3 As Range, Table5 As Range, F26 As Range)
getSomeData = ""
If WorksheetFunction.VLookup(E3, Table5, 2, 0) >= F26 Then
getSomeData= WorksheetFunction.VLookup(E3, Table5, 4, 0) * F26
End If
End Function
Than you can call it in any cell just typing =getSomeData(E3;Tabel5;F26)
in it. Advantages of this approach is that when you change data in related cells (E3
, F26
or Table5
), function will recalculate the value in cell.
你可以在任何单元格中调用它,只需输入=getSomeData(E3;Tabel5;F26)
它。这种方法的优点是当您更改相关单元格(E3
,F26
或Table5
)中的数据时,函数将重新计算单元格中的值。
Second way:
第二种方式:
Sub getSomeDataSub()
Dim E3 As Range, F26 As Range, Table5 As Range
Range("F27") = ""
Set E3 = Range("E3")
Set Table5 = Range("Table5")
Set F26 = Range("F26")
If WorksheetFunction.VLookup(E3, Table5, 2, 0) >= F26 Then
Range("F27")= WorksheetFunction.VLookup(E3, Table5, 4, 0) * F26
End If
End Sub
you can call this macros from the ribbon and it will put some data in F27
cell. In this approach you should call this macro each time you change values in related cells to recalculate it in F27
.
您可以从功能区调用此宏,它会将一些数据放入F27
单元格中。在这种方法中,每次更改相关单元格中的值时都应调用此宏以在F27
.