Consulting

Results 1 to 14 of 14

Thread: Treeview - Sort Nodes Alphabetically (Like in Windows)

  1. #1
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location

    Treeview - Sort Nodes Alphabetically (Like in Windows)

    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)
    userform.jpg

    Windows Explorer: (Sorted Alphabetically)
    explorer.jpg

    Im using windows 7
    VBA 7.1
    VBA Knowledge: Beginner

    Cheers,


    Luke Kelsen

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    they are all text strings that contain numbers.

    below you will find the answer:
    https://msdn.microsoft.com/en-us/lib...(v=vs.60).aspx
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Why don't you use

    Application.filedialog(3).show

  4. #4
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location
    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.


    Quote Originally Posted by snb View Post
    Why don't you use

    Application.filedialog(3).show
    Sorry snb i'm not quite sure what you trying to say?

    Cheers,


    Luke

  5. #5
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location
    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

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Did you run the code I gave you ??

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    one option can be renaming folders in desired sort order.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location
    Quote Originally Posted by snb View Post
    Did you run the code I gave you ??
    Hi snb, I'm not quite sure what I'm meant to do with it sorry?

  9. #9
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location
    Quote Originally Posted by mancubus View Post
    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?

  10. #10
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    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
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  11. #11
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location
    Quote Originally Posted by mancubus View Post
    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

  12. #12
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    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.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  13. #13
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location
    Quote Originally Posted by mancubus View Post
    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

  14. #14

    Use the sorted property

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •