简体   繁体   中英

how to write an excel formula in vba?

I'm new in vba. I have an excel formula that I want to write it as vba code. 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. 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.

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.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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