+ Reply to Thread
Results 1 to 13 of 13

VBA cascading listboxes.

  1. #1
    Registered User
    Join Date
    07-02-2010
    Location
    Del
    MS-Off Ver
    Excel 2007
    Posts
    11

    Question VBA cascading listboxes.

    i want to make a user interface which has listboxes in it. the user selects the root folder and the following listboxes display the subfolders/files in the folders based on what list item user clicks on.

    List box 1 always has the root folder, listbox 2 displays the name of subfolders and/or files (names only) in them sorted by folders first and then files(filter for ".xls*") and so on until the last listbox which displays the name of last folder and/or files in that folder. the whole path is displayed in the label which is shown at a separate place in the form. my problem is that i am not able to make the listboxes dynamic i.e. how to change the current list items in a list box based on what user selects in previous listbox.

    Any help will be greatly appreciated. thanks in advance.
    Last edited by harp; 07-27-2010 at 04:08 PM. Reason: solved

  2. #2
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA cascading listboxes.

    Hi harp;

    This is an interesting idea. I think I just might design this for myself.

    When you say form do actually want it on a userform, or a form on a sheet? Putting it on a userform might be a lot more difficult than putting it on a sheet. On a userform, I think I would explore the idea of just having one listbox. As the user selects an item in the listbox, it would add the selection to the label and then repopulate itself with the folders/files in the selection.

    I don't have the time to do this right now, but maybe Friday or this weekend if no one else does it.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  3. #3
    Registered User
    Join Date
    07-02-2010
    Location
    Del
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA cascading listboxes.

    yea.. this is interesting. i am doing this as a project and right now I am able to get the list of all folders by browsing through them. as something is found, it is stored in an array and then used for displaying later. however i am not able to display this list in my listbox.

    btw, by Form, i mean userform. that's more intuitive for a novice user instead of a form on an excel sheet. the users at my end are typically faced with " where should I click next??" type of question. so making life easy for them.

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA cascading listboxes.

    Hi harp;

    I still don't have time, but if you check out AddItem, RemoveItem, and ListCount in help. It should get you going.

  5. #5
    Registered User
    Join Date
    07-02-2010
    Location
    Del
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA cascading listboxes.

    i am having trouble finding the all folders and all files in a given directory path. my root path remains fixed. i am unable to loop thorugh all subfolders and files within each subfolder and display them. can you help please?


    Sub ProcessFiles(strFolder As String, strFilePattern As String)
    Dim strFileName As String
    Dim strFolders() As String 'Array for Folder names
    Dim strFiles() As String 'Array for filename
    Dim iFolderCount As Integer
    Dim iFilesCount As Integer
    Dim i As Integer

    'Collect child folders
    strFileName = Dir(strFolder & "\", vbDirectory)

    Do Until strFileName = ""
    If (GetAttr(strFolder & "\" & strFileName) And vbDirectory) = vbDirectory Then

    If Left$(strFileName, 1) <> "." Then ' check if its a folder or file

    ReDim Preserve strFolders(iFolderCount) ' if folder then add to folders array

    strFolders(iFolderCount) = strFolder & "\" & strFileName '

    iFolderCount = iFolderCount + 1
    Else

    ReDim Preserve strFiles(iFilesCount)
    strFiles(iFilesCount) = strFolder & "\" & strFileName
    iFilesCount = iFilesCount + 1
    End If

    End If
    strFileName = Dir$()

    Loop

    'process files in current folder
    strFileName = Dir$(strFolder & "\" & strFilePattern)


    Do Until strFileName = ""

    ListBox2.AddItem strFolder & "\" & strFileName

    strFileName = Dir(Dir$())

    Loop

    'Look through child folders

    For i = 0 To iFolderCount - 1

    ProcessFiles strFolders(i), strFilePattern 'call processfiles to get files in the folders stored in the array

    Next i

  6. #6
    Registered User
    Join Date
    07-02-2010
    Location
    Del
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA cascading listboxes.

    it does not loop through all subfolders and all files within all fodlers. just gives one file each folder

  7. #7
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA cascading listboxes.

    Hi harp;

    It is against the rules to reply to any one that does not use code tags in their posts.

    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # button at the top of the post window. If you are editing an existing post, press Go Advanced to see the # button.

    Once you have put code tags around your code, I can reply.

  8. #8
    Registered User
    Join Date
    07-02-2010
    Location
    Del
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA cascading listboxes.

    Please Login or Register  to view this content.


    to simulate this, make a folder and set its path. then create subfolders and within them again subfolder. atleast three levels within each.
    Last edited by harp; 07-23-2010 at 10:22 AM. Reason: additional information

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: VBA cascading listboxes.

    As a matter of interest, why use listboxes for this rather than a folder browser?
    Remember what the dormouse said
    Feed your head

  10. #10
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA cascading listboxes.

    Hi harp;

    Try this file.
    Attached Files Attached Files
    Last edited by foxguy; 07-23-2010 at 11:26 PM.

  11. #11
    Registered User
    Join Date
    07-02-2010
    Location
    Del
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA cascading listboxes.

    Foxguy: thansk for that. It helped a lot. I am still not able to see the files in the folders based on the value in text box. my files are usually "*.xl*" i.e xls or xlsm or xlsx. but i dont get to see any files which have these extns. i see only xls.

    But I must say that it is very close to what i am trying to do. i now need to pass the path of the file in the caption to my other code in macro. how do i do that?

  12. #12
    Registered User
    Join Date
    07-02-2010
    Location
    Del
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA cascading listboxes.

    Foxguy: thansk for that. It helped a lot. I am still not able to see the files in the folders based on the value in text box. my files are usually "*.xl*" i.e xls or xlsm or xlsx. but i dont get to see any files which have these extns. i see only xls.

    I must say that it is very close to what I am trying to do.
    Ammendment: I now need to pass the path of the file in the MsbBox to my other code in macro. how do i do that?

  13. #13
    Registered User
    Join Date
    07-02-2010
    Location
    Del
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA cascading listboxes.

    I got this done. changed my attritonsample function to pass two parameters

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1