[英]how to write an excel formula in vba?
我是vba的新手。 我有一个Excel公式,我想将其编写为VBA代码。 但我对此有疑问。 我不知道该怎么做。 有谁能够帮助我? 这是公式:
IFERROR(LOOKUP(2^15,SEARCH(G$6:G$8,B6),G$6:G$8),"")
实际上,我在sheet2的G列中有一些关键字,并且我想在包含文本的sheet1的B列中搜索它们。 如果有任何匹配,我希望vba代码在第一张工作表的一列(例如D)中返回匹配关键字,如果没有,则将对应的单元格留空。
我不知道该怎么做。 有谁能够帮助我?
即使您提供的描述似乎与您提供的功能不匹配,我也会对此采取行动。
我IsError
函数与Application.Match
一起使用,以检查是否在Sheet1的Range(“ B:B”)中找到了lookup_value
。
Dim lookup_value as String ' the value you're searching for.'
Dim found_value as String ' the value to return if a match is found.'
lookup_value = "edit this value!" '<~~ This is the value you're searching for. Edit as needed.'
If Not IsError(Application.Match(lookup_value, Sheets("Sheet1").Range("B:B"),False) Then
'If the above does not yield an error, then set the found_value based on VLOOKUP.'
found_value = Application.WorksheetFunction.VLookup(lookup_value, Sheets("Sheet1").Range("B:D"),2,False)
Else:
'If the MATCH function returns an error, set the found_value = vbNullString.
found_value = vbNullString
End If
根据此结果,您可以简单地将一个单元格值设置为函数found_value
的结果。
ActiveCell.Value = found_value
或Range("A1").Value = found_value
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.