簡體   English   中英

如何在VBA中編寫Excel公式?

[英]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_valueRange("A1").Value = found_value等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM