简体   繁体   English

如何在VBA中编写Excel公式?

[英]how to write an excel formula in vba?

I'm new in vba. 我是vba的新手。 I have an excel formula that I want to write it as vba code. 我有一个Excel公式,我想将其编写为VBA代码。 But I have a problem with that. 但我对此有疑问。 I do not know how to do that. 我不知道该怎么做。 Can anybody help me? 有谁能够帮助我? here is the formula: 这是公式:

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

Actually I have some keywords in column G from sheet2 and I want to search them in column B from sheet1, which contains text. 实际上,我在sheet2的G列中有一些关键字,并且我想在包含文本的sheet1的B列中搜索它们。 If there is any match I want that vba code returns the matched keyword in a column (for example D) in first sheet, if not leaves the corresponding cell empty. 如果有任何匹配,我希望vba代码在第一张工作表的一列(例如D)中返回匹配关键字,如果没有,则将对应的单元格留空。

I do not know how to do that. 我不知道该怎么做。 Can anybody help me? 有谁能够帮助我?

I'm going to take a stab at this even though the description you provide does not seem to match the function you provide. 即使您提供的描述似乎与您提供的功能不匹配,我也会对此采取行动。

I use the IsError function with the Application.Match first to check if the lookup_value is found in the Range("B:B") on Sheet1. 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

From this result, you can simply set a cell value to the result of the function, found_value . 根据此结果,您可以简单地将一个单元格值设置为函数found_value的结果。

ActiveCell.Value = found_value or Range("A1").Value = found_value , etc. ActiveCell.Value = found_valueRange("A1").Value = found_value等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM