简体   繁体   中英

Evaluate nested functions in Excel VBA SUMPRODUCT and TRIM

My goal is to compare a value to another which contains an indeterminate number of spaces after the value.

For example, I am looking for "apple" value in column D and would like to return the sum of the correlated row in column G. The "apple" value may appear multiple times in column D and each instance may have a different number of white spaces concatenated to the end ie"apple" or "apple ".

I tried:

MyValue = ActiveWorkbook.Sheets(MySheet).Evaluate("SUMPRODUCT(--(TRIM($D:$D)=""" & Txt & """),$G:$G)")

Where Txt would be "apple" in this example, column D contains a list of fruits, and column G contains the number of fruits.

I may be misplacing my double-quotes within the equation, but I don't understand why the quotes are needed where they are needed.

Was able to find a solution from another source:

ActiveWorkbook.Sheets(MySheet).Range("Z1").Formula = "=SUMPRODUCT(--(TRIM($D:$D)=""" & Txt & """),$G:$G)"
ActiveWorkbook.Sheets(MySheet).Range("Z1").Calculate
MyValue = ActiveWorkbook.Sheets(MySheet).Range("Z1").Value

Turns out the Evaluate function is not as powerful as the Formula function.

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