I'm not sure where to even start.
First, the code you came up with in post #3 has several issues with it. I don't know how to fix them, because you didn't show the entire function, and because I really don't know what you're trying to do.
Second, yes you can incorporate multiple variables in the macro. The TEXTJOINX macro shows how to do that, first you have a loop to look at the passed parameters and see what type they are, next each parameter is handled according to its type, which may be another loop. The TEXTJOINX macro assumes the parameters are strings, arrays of strings, or ranges of strings. But in your case, you seem to be looking at values. This is fine, it just depends on what your ultimate goal is.
Third, you may want to look into using Excel functions in your VBA code. The line
VBA Code:
MySum = WorksheetFunction.Sum(Parm1)
will work regardless of whether the parameter is a range, array or constant. Or even:
VBA Code:
MySum = WorksheetFunction.Sum(Parm1, Parm2, Parm3, Parm4, Parm5)
Depending on your requirements, and if there is an existing built-in function, you might be able to simplify things a lot.
Fourth, I'm not exactly sure what that AI statement is saying, but I have a suspicion. Using the same macro I provided in post #2, consider this:
Book2 |
---|
|
---|
| A | B | C | D | E | F | | | | | | | | | | | Q | R |
---|
1 | 1 | | 15 | 25 | | | | | | | | | | | | | | |
---|
2 | 2 | | 15 | 25 | | | | | | | | | | | | | | |
---|
3 | 3 | | 15 | | | | | | | | | | | | | | | |
---|
4 | 4 | | | | | | | | | | | | | | | | | |
---|
5 | 5 | | | | | | | | | | | | | | | | | |
---|
6 | | | | | | | | | | | | | | | | | | |
---|
7 | | | | | | | | | | | | | | | | | | |
---|
8 | | | | | | | | | | | | | | | | | | |
---|
9 | | | | | | | | | | | | | | | | | | |
---|
10 | | | | | | | | | | | | | | | | | | |
---|
11 | | | | | | | | | | | | | | | | | | |
---|
12 | A | | | 1 | | | | | | | | | | | | | A | 1 |
---|
13 | B | | | 2 | | | | | | | | | | | | | C | 3 |
---|
14 | C | | | 3 | | | | | | | | | | | | | E | 5 |
---|
15 | D | | | 4 | | | | | | | | | | | | | G | 7 |
---|
16 | E | | | 5 | | | | | | | | | | | | | I | 9 |
---|
17 | F | | | 6 | | | | | | | | | | | | | | |
---|
18 | G | | | 7 | | | | | | | | | | | | | | |
---|
19 | H | | | 8 | | | | | | | | | | | | | | |
---|
20 | I | | | 9 | | | | | | | | | | | | | | |
---|
21 | J | | | 10 | | | | | | | | | | | | | | |
---|
|
---|
This shows that a construction like INDEX(D12#,MATCH(Q12#,A12#,0)) CAN be passed as a parameter to a function. It will be passed as an array. The exception is when the Q12# range returns only a single value. In this case (for whatever reason the Excel developers came up with), it will be passed as a 1-cell range. You can see this if you change the Q12 formula to
and step through the macro. With 1 cell, it will take the range branch, with more, the array branch. There is actually a pretty easy workaround, if you're working with numerical values. Just add 0 to the range (if the range only contains numbers), and Excel will convert it to an array, even if it's only 1 cell. This should simplify your function enormously, since you'll only have to handle arrays, not ranges or constants. So like this:
Rich (BB code):
=test1(R12#+0)
=test1(INDEX(D12#,MATCH(Q12#,A12#,0))+0)
Fifth and finally, I tried to make a useful version of =INDEX(D12#,MATCH(Q12#,A12#,0)), but got confused quickly, so I just came up with a generic version that uses that formula, but doesn't really do much. Every time I tried to do a useful version, I came up with a shorter version that didn't require that construction. So instead of posting some code that you admit doesn't work, it would be more useful if you were to show some of your data, and explain what you want to do with it.
Sorry about the long post, but I'm trying to be helpful with limited data, which means there are lots of options.