Your function doesn't output the end brackets
I decided to respond to your last comment first because that will be the shorter answer. I did not think parentheses were part of what the OP wanted, rather, I thought he used them to show what he wanted as an aside. If the OP did want the parentheses, we would just concatenate them onto the two ends of what is assigned to the function name (SpaceLoc). By the way, my use of parentheses to encase the function name as an aside in order to "explain" what I meant by "function name" is how I thought the OP used them to explain what row numbers he expected back as his answer. Anyway, this what the single code line would look like with them (the parentheses)...
Code:
[table="width: 500"]
[tr]
[td]SpaceLoc = Replace(Application.Trim(Join(Application.Transpose(Evaluate("IF(MID(""" & S & """,ROW(1:" & Len(S) & "),1)="" "",Row(1:" & Len(S) & "),"""")")), " ")), " ", ", ")[/td]
[/tr]
[/table]
That's pretty nice Rick. I sort of understand the evaluate onwards but the rest, I don't. If U have some time to say what's up with transpose, join, trim and replace?
I'll try and I'll use this color coded "exploded" view of that single code line for reference...
Code:
SpaceLoc = [B][COLOR="#000000"]Replace([/COLOR][/B]
[B][COLOR="#EE82EE"]Application.Trim([/COLOR][/B]
[B][COLOR="#8B4513"]Join([/COLOR][/B]
[B][COLOR="#0000FF"]Application.Transpose([/COLOR][/B]
[B][COLOR="#008000"]Evaluate([/COLOR][/B]
[B][COLOR="#FF0000"]"IF(MID(""" & S & """,ROW(1:" & Len(S) & "),1)="" "",Row(1:" & Len(S) & "),"""")"[/COLOR][/B]
[B][COLOR="#008000"])[/COLOR][/B]
[B][COLOR="#0000FF"])[/COLOR][/B]
[B][COLOR="#8B4513"]," ")[/COLOR][/B]
[B][COLOR="#EE82EE"])[/COLOR][/B]
[B][COLOR="#000000"]," ", ", ")[/COLOR][/B]
My explanation will start in the middle and work outward because that is the way the VB compiler processes it (each function call is evaluated and its result is feed to the immediately surrounding function for use when it is processed and so on until the outer most function is processed (at which point the answer is assigned to the function name (SpaceLoc) for return to the calling code (which is either a formula on the worksheet or another VB procedure). Okay, let's start.
The Evaluate function (the red text) is basically an array formula processor although it can also handle non-array formulas as well. Here, we will give it an array formula and it will return an array of values. You already said you kind of figured out the formula the Evaluate function is evaluating, but let me summarize it for other future readers of this thread. Let's assume the cell text being fed to the SpaceLoc function (via the S argument) is "
One Two Three Four" (so the final answer should be
4, 8, 14). Note that the length of this text is 18 characters long. Here is what that ugly looking text string argument to the Evaluate function becomes once the substitutions for S are made...
Code:
IF(MID("One Two Three Four",ROW(1:18),1)=" ",Row(1:18),"")
so the Evaluate function will iterate the text string from its first character to its last character (character positions 1 through 18)... if it finds a space character, it return outputs the position of the space to the array it returns otherwise it returns the empty text string ("") to the array it returns. The Transpose function takes that vertical array of values and converts it a one-dimensional horizontal array (the array needs to be one-dimensional because the next function in the chain of function is the Join function which can only operate on one-dimensional arrays. Join is a concatenate-type of function which takes each element in the array given to it and makes a text string out of those elements putting the specified delimiter (its second argument which, in this case, is a space character). The result is this text string (I put quote around it so you can see that there are multiple leading and trailing spaces as well as multiple internal spaces)...
The next function up the chain of functions evokes the worksheet's TRIM function (we do this because unlike VB's Trim function, the worksheet TRIM function removes outer spaces and collapse all interior multiple space down to single spaces. The result of this operation returns this text string...
The last function in the chain replaces those single spaces with a comma space so that the outputted text string returned from the function "looks nice". Okay, that's it... simple, right?
Kidding aside, I hope it was somewhat clear to you how my code is working to return what the OP requested.