简体   繁体   English

Excel VBA-用户定义函数-将与数组相关的公式(常规公式上为CTRL + SHIFT + ENTER)转换为UDF

[英]Excel VBA - User Defined Function - converting array related formula (CTRL + SHIFT + ENTER on a normal formula) to a UDF

I have been passing by several threads that are quite similar to my problem. 我一直在绕过与我的问题非常相似的几个线程。 However, there are various interpretations which may not be applicable to my case. 但是,有多种解释可能不适用于我的情况。 I have this "regular" excel formula: 我有这个“常规” excel公式:

={IF(OR(ISNUMBER(SEARCH($O$2:$O$2000,A2))), INDEX($P$2:$P$2000, MATCH(TRUE, ISNUMBER(SEARCH($O$2:$O$2000, A2)),0)), "")}

I wanted to convert this to Excel VBA function with the following parameters 我想使用以下参数将其转换为Excel VBA函数

FUNCTION ADVLOOKUP(TAG, SENTENCESTOLOOKAT, WORDSTOLOOKUP)

TAG = represents Column P on the ORIGINAL FORMULA which will be put beside each cell/row representing the SENTENCESTOLOOKAT TAG =表示原始公式中的P列,该列将置于表示SENTENCESTOLOOKAT的每个单元格/行旁边

SENTENCESTOLOOKAT = represents A2 or any cell on column A to be targeted by the formula. SENTENCESTOLOOKAT =表示A2或公式要定位的列A上的任何单元格。 This is a sentence which contains any 1 of the WORDSTOLOOKUP 这是一个包含WORDSTOLOOKUP中的任何一个的句子

WORDSTOLOOKUP = represents Column O which is the inventory of words to be searched checked on each of the SENTENCESTOLOOKAT WORDSTOLOOKUP =表示列O,它是每个SENTENCESTOLOOKAT上要检查的要搜索单词的清单

Sorry, I'm unable to post the Excel file because apparently I have low reputation/rep points. 抱歉,我无法发布Excel文件,因为显然我的信誉/代表得分很低。 :( But I do appreciate if you can advice whether the said Excel formula can be converted to a UDF. :(但是,如果您能建议上述Excel公式是否可以转换为UDF,我将不胜感激。

Thank you! 谢谢!

To only return one value, you could use a simple call to Evaluate: 要仅返回一个值,可以使用一个简单的调用来评估:

Function ADVLOOKUP(TAG As Range, SENTENCESTOLOOKAT As Range, WORDSTOLOOKUP As Range)
    ADVLOOKUP = SENTENCESTOLOOKAT.Worksheet.Evaluate("INDEX(INDEX(" & TAG.Address & ",MATCH(TRUE,ISNUMBER(SEARCH(" & _
                    WORDSTOLOOKUP.Address & "," & SENTENCESTOLOOKAT.Address & ")),0)),)")
End Function

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

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