PDA

View Full Version : [SOLVED] Treeview - Sort Nodes Alphabetically (Like in Windows)



lkelsen
05-19-2015, 08:46 PM
Hi team,

Following on from my last thread regarding populating a TreeView with windows directories, VBA has the tendency to sort the nodes in numerical order where I need to sort them alphabetically as they would appear in their windows directory folder.

Has anybody got a clue on how to do this?


The TreeView.Sorted = True

The above seems to have no effect, is there a way to stipulate how it is sorted e.g. Numerically, Alphabetically, Ascending, Descending?

As this is only my fourth post here at vbaexpress the next post contains my original thread addressing the population of my TreeView and also a link to the ready to use file in which i manipulated to suit my requirements.

I have also attached images of how it looks on my userform and how the order should look as per the windows folder.

Userform: (Sorted Numerically)
13439

Windows Explorer: (Sorted Alphabetically)
13440

Im using windows 7
VBA 7.1
VBA Knowledge: Beginner

Cheers,


Luke Kelsen

mancubus
05-20-2015, 01:35 AM
they are all text strings that contain numbers.

below you will find the answer:
https://msdn.microsoft.com/en-us/library/aa259700(v=vs.60).aspx

snb
05-20-2015, 02:02 AM
Why don't you use


Application.filedialog(3).show

lkelsen
05-20-2015, 06:25 PM
Mancubus,

As in the Nodes are text strings which each have an assigned index number of 0, 1, 2, 3 and so on.... ?

If so then how would I code it in a way that whenever a folders text string starts with a number e.g. "1 - Estimates and Proposals" that it would sort from say 1 through to 17 etc. and then in each sub-folder if there are some folders with text strings starting with being from numbers 1 through to 6 for arguments sake that it would sort those in numerical order e.g. with folder "1 - Client" at the top and so on...

Sorry my understanding of VBA is very limited and I am learning pretty much from search engines and forums such as these. Would it be helpful if I upload my Excel file?
I also wouldn't even know where to put a sort function in my code.

Apologies for the image sizes above and the link i promised which I am not yet allowed to include in my posts.



Why don't you use


Application.filedialog(3).show

Sorry snb i'm not quite sure what you trying to say?

Cheers,


Luke

lkelsen
05-20-2015, 06:42 PM
Just a thought, when the TreeView is populated, i'm guessing it grabs the first folder/file object in the specified windows directory, assign it index number "0" add it to the TreeView then moves to the next folder/file object in the specified directory and assign it index number "1"... and so on?

If I could find a way for the TreeView to be sorted by its index number from 0 through to X number of nodes, would you guys know if this is possible?

Cheers,


Luke

snb
05-21-2015, 12:21 AM
Did you run the code I gave you ??

mancubus
05-21-2015, 01:29 AM
one option can be renaming folders in desired sort order.

lkelsen
05-21-2015, 05:26 AM
Did you run the code I gave you ??

Hi snb, I'm not quite sure what I'm meant to do with it sorry?

lkelsen
05-21-2015, 05:27 AM
one option can be renaming folders in desired sort order.

Mancubus, the folders are already sorted in the correct order in the windows directory if that's what you mean?

mancubus
05-21-2015, 05:52 AM
No.

when sorted, the two screenshots you posted would be the same.

Your folder names
1 - blah blah
2 - blah blah
...
10 - blah blah
...
17 - blah blah


sorted folder names
01 - blah blah
02 - blah blah
...
10 - blah blah
...
17 - blah blah

lkelsen
05-21-2015, 02:01 PM
No.

when sorted, the two screenshots you posted would be the same.

Your folder names
1 - blah blah
2 - blah blah
...
10 - blah blah
...
17 - blah blah


sorted folder names
01 - blah blah
02 - blah blah
...
10 - blah blah
...
17 - blah blah

Sorry, yes that seems a much more feasible solution and such a simple one. Thank you once again!

Will have to update our entire history of projects within our file system but that's not a problem.

Cheers once again for your help gentlemen,


Luke

mancubus
05-21-2015, 02:15 PM
welcome again.

i dont know why windows explorer display in numerical order the folders whose names start with a number. even they include numbers they are strings.

lkelsen
05-21-2015, 07:09 PM
welcome again.

i dont know why windows explorer display in numerical order the folders whose names start with a number. even they include numbers they are strings.

I guess Microsoft may have programmed explorer to identify folder/file names where the string contains numbers at the start of the folder/file name, and then sort integers before text? Just a guess :cool:

geekforever
11-19-2018, 02:15 AM
Hi,

If you want to sort your treeview you should use " yournodes.sorted=true" where yournodes is the parent node of the nodes you try to sort .
If you don't have root node just use "Treeview.sorted=true"


Good luck