Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
MS-Off Ver
MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
Posts
91,087
Re: How to auto sort a series in a single cell
Can't be done without VBA - shall I move the thread for you?
Ali Enthusiastic self-taught user of MS Excel who's always learning! Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy. You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.
NB: as a Moderator, I never accept friendship requests. Forum Rules (updated August 2023): please read them here.
According to your attachment as a beginner starter :
PHP Code:
Sub Demo1()
Const S = ","
Dim V, C%, W, N&
With [B2:C2]
V = .Value2
For C = 1 To .Count
With New Collection
For Each W In Split(V(1, C), S)
W = Val(W)
For N = 1 To .Count
If .Item(N) > W Then .Add W, , N: N = 0: Exit For
Next
If N Then .Add W
Next
V(1, C) = .Item(1)
For N = 2 To .Count: V(1, C) = V(1, C) & S & .Item(N): Next
End With
Next
.Value2 = V
End With
End Sub
Do you like it ? So thanks to click on bottom left star icon « ★ Add Reputation » !
According to your attachment a starter demonstration via Excel basics formula :
PHP Code:
Sub Demo2()
Const S = ","
Dim V, C&
With [B2.C2]
V = .Value2
For C = 1 To .Count
V(1, C) = Join(Evaluate("IF({1},SMALL({" & V(1, C) & "},COLUMN(" & [A1].Resize(, UBound(Split(V(1, C), S)) + 1).Address & ")))"), S)
Next
.Value2 = V
End With
End Sub
Do you like it ? So thanks to click on bottom left star icon « ★ Add Reputation » !
I have been using Evaluate for years now and I don't think I have come across, or at least noticed, this before. I was trying to simplify the above line of code and kept erroring out, so I decided to strip it down to the bare essentials with the idea of building it up from there. That is when I got stumped. In its barest form, your formula becomes this...
The resulting text string argument fed to the Evaluate function is identical to the resulting text string argument that your code feeds to the Evaluate function; however, your code runs and the one I wrote generates a "Type Mismatch" error? Just to reiterate, when I put a breakpoint on that line of code and execute these two statements (the text string argument for the Evaluate function)...
Nope ‼ chrubble can't consider your Windows ArrayList on his MAC version ! The reason why I started with a VBA Collection …
Originally Posted by Rick Rothstein
they both print out the identical text string. So my question... why does the Evaluate function care how its text string argument is constructed?
Nope again as the Evaluate method does not care about the string construction but just about its value !
So your bad as you forgot to check out for the second cell, C2 !
Just using Debug.Print you will see your formula is not the same as mine …
Sub Demo2a()
Const F = "IF({1},SMALL({¤},COLUMN(A:#)))", S = ","
Dim V, C&
With [B2.C2]
V = .Value2
For C = 1 To .Count
V(1, C) = Join(Evaluate(Replace (Replace(F, "¤", V(1, C)), "#", Chr(UBound(Split(V(1, C), S)) + 65))), S)
Next
.Value2 = V
End With
End Sub
Do you like it ? So thanks to click on bottom left star icon « ★ Add Reputation » !
Hi Marc L - Thanks for the response . How do I add this into VBA? As a new module? Sorry, I'm a complete novice with VBA. Currently my VBA project looks like this... Screenshot 2019-10-28 at 10.44.46.png
All the best,
chrubble
Re: VBA: How to auto sort a series in a single cell
Originally Posted by rorya
I'd suggest you insert a new module (or use the existing one) and just copy and paste it in.
Thanks Rory.
The structure of my VBA Project is posted above - Which directory or folder should I create the module in? The 'Modules' sub-directory of the 'VBAProject (Sample_1294080_Mac.xlsb)' project?
Re: VBA: How to auto sort a series in a single cell
Thanks Rory and Mark,
I added the code as a new module (Module 2) under the 'VBAProject (Sample_1294080_Mac.xlsb)' project. But when I try to run it, I get a run-time '424' (Object required) error. The debug shows that the issue is the 'V = .Value2' line in the code.
Bookmarks