0

我是vba的新手。我有一个excel公式,我想把它写成vba代码。但我有一个问题。我不知道该怎么做。有谁能够帮我?这是公式:

IFERROR(LOOKUP(2^15,SEARCH(G$6:G$8,B6),G$6:G$8),"")

实际上,我在 sheet2 的 G 列中有一些关键字,我想在 sheet1 的 B 列中搜索它们,其中包含文本。如果有任何匹配项,我希望 vba 代码在第一张表的列(例如 D)中返回匹配的关键字,如果没有,则将相应的单元格留空。

我不知道该怎么做。有谁能够帮我?

4

1 回答 1

1

即使您提供的描述似乎与您提供的功能不匹配,我也会对此进行尝试。

我使用第一个IsError函数Application.Match来检查是否lookup_value在 Sheet1 的 Range("B:B") 中找到。

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

于 2013-04-22T17:05:34.300 回答