[英]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.