+ Reply to Thread
Results 1 to 26 of 26

Help With Alphabetical Order In Listbox

Hybrid View

  1. #1
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Help With Alphabetical Order In Listbox

    Hello, i have a user form which i use to sort out my pattern numbers however as i have been adding to my spreadsheet and there gets more and more i need to find a way to sort them alphabetically.

    Snag_1b9d3fe7.png

    This is My Spreadsheet

    Snag_1ba000cc.png

    This is My userform

    Listbox is called ListboxA
    A-Z Button is called AAZ, to click to sort if alphabetically, or for it to automatically do it
    Userform is called PatternsA

    when its sorted alphabetically i would like the numbers to go with them if thats possible?
    Attached Images Attached Images

  2. #2
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    Private Sub UserForm_Initialize()
    Dim lrow As Long
    Dim rng As Range
    lrow = Range("a" & Rows.Count).End(xlUp).Row
    Set rng = Range("A1:b" & lrow)
    rng.Sort key1:=Range("B1:B" & lrow), _
       order1:=xlAscending, Header:=xlNo
    ListBoxA.List = rng.Value
    End Sub
    and to put back in order on worksheet

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Dim lrow As Long
    Dim rng As Range
    lrow = Range("a" & Rows.Count).End(xlUp).Row
    Set rng = Range("A1:b" & lrow)
    rng.Sort key1:=Range("a1:a" & lrow), _
       order1:=xlAscending, Header:=xlNo
    End Sub
    Last edited by nigelog; 09-06-2019 at 04:37 AM.

  3. #3
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Re: Help With Alphabetical Order In Listbox

    thank you for such a fast responce and it works what do i need to change to do the same with b, i changed it to ListBoxB, but its not working?

  4. #4
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: Help With Alphabetical Order In Listbox

    Good morning VBTroubles

    This link demonstrates how to sort the items within the listbox using a bubblesort technique (look about halfway down the page), this will not alter the sequence of your source list.

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  5. #5
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    what range does listbox b refer to??

  6. #6
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Re: Help With Alphabetical Order In Listbox


  7. #7
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: Help With Alphabetical Order In Listbox

    Hi VBTroubles

    It would be easier for all concerned if you were to post a workbook with what you have so far. Anyone looking at this post will have to recreate what you already have to help out.

    HTH

    DominicB



    Please attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  8. #8
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Re: Help With Alphabetical Order In Listbox

    ill just send the file,

    Snag_1bd74df7.png

    not too sure where to go from here?

    I just need it to sort everything but cant get it to work properly
    Attached Files Attached Files

  9. #9
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    That workbook is annoyingly set to show the menus only and when you can see the worksheet the menu is modal so can't get further. I see that the menu loads the listbox by a letter variable. Probably the same listbox. I'll leave as not in a position to turn off macros etc to look at it

  10. #10
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Re: Help With Alphabetical Order In Listbox

    if you click on add pattern then you can close the form to get to the spreadsheet, sorry about that i removed the ability to close it as my uncle kept clicking it leaving the workbook open in the background

  11. #11
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    when i get to add pattern I see no spreadsheet here. Same with see spreadsheet option - I can see in background but userform close turned off

  12. #12
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Re: Help With Alphabetical Order In Listbox

    click open spreadsheet, then add pattern then the x to close, sorry its a pain i know

  13. #13
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    Sorted.
    You have a userform for each letter which is unnecessary.
    You can select the letter from the choice menu and pass the letter as a variable via a class module on the buttons to one userform with the listbox populated by a filtered and sorted list.

    As a project I would love to complete but travelling this weekend.

    The outline is above if anyone would care to assist with what VBTroubles has to date or tidying into a new method

  14. #14
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Re: Help With Alphabetical Order In Listbox

    okay =], sorry when i made this it was such a long time ago and i havnt used vb since then so everything i use to know i pretty much forgot and am trying to learn bit by bit again

  15. #15
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    just spotted this

    put your corresponding sheet name into each sub
    and use this code in each userform

    Private Sub UserForm_Initialize()
    Dim lrow As Long
    Dim rng As Range
    With Sheets("Sheet1") ''''''Enter each proper sheetname here
    lrow = .Range("a" & Rows.Count).End(xlUp).Row
    Set rng = .Range("A1:b" & lrow)
    rng.Sort key1:=Range("B1:B" & lrow), _
       order1:=xlAscending, Header:=xlNo
    ListBoxA.List = rng.Value '''''change listbox name if necessary
    End With
    End Sub
    that would be the quickest way get you up and running

  16. #16
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    drop this code into each userform

    Private Sub UserForm_Initialize()
    Dim var
    var = Right(Me.Caption, 1)
    With Sheets("Sheet" & var)
    Debug.Print .Name
    Dim lrow As Long
    Dim rng As Range
    lrow = Range("a" & Rows.Count).End(xlUp).Row
    Set rng = Range("A1:b" & lrow)
    rng.Sort key1:=Range("B1:B" & lrow), _
       order1:=xlAscending, Header:=xlNo
    Controls("ListBox" & var).List = rng.Value
    End With
    End Sub
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Dim lrow As Long
    Dim rng As Range
    Dim var
    var = Right(Me.Caption, 1)
    With Sheets("Sheet" & var)
    lrow = Range("a" & Rows.Count).End(xlUp).Row
    Set rng = Range("A1:b" & lrow)
    rng.Sort key1:=Range("a1:a" & lrow), _
       order1:=xlAscending, Header:=xlNo
    End With
    End Sub
    I cant test ....workbook will not sit still lol
    Last edited by nigelog; 09-06-2019 at 06:10 AM.

  17. #17
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Re: Help With Alphabetical Order In Listbox

    Attachment 640317

    Thanks am trying it now, any reason why its leaving blanks and loading the numbers in so slowly?

    and i am having duplicate numbers

  18. #18
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    see post 16

  19. #19
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Re: Help With Alphabetical Order In Listbox

    nope not working =[

  20. #20
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,410

    Re: Help With Alphabetical Order In Listbox

    Please attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  21. #21
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    this is working here

    change your sheet item numbering to numbers not a formula. When adding new pattern increment by 1 in the code

    I have used for half of the letters with no problem when that is changed

    in each userform
    Private Sub UserForm_Initialize()
    Dim var
    Dim arr As Variant
    var = Right(Me.Caption, 1)
    Sheets("Sheet" & var).Activate
    Dim lrow As Long
    Dim rng As Range
    lrow = Range("a" & Rows.Count).End(xlUp).Row
    Set rng = Range("A1:b" & lrow)
    rng.Sort key1:=Range("B1:B" & lrow), _
       order1:=xlAscending, Header:=xlNo
    arr = rng.Value
    Controls("ListBox" & var).List = arr
    Set arr = Nothing
    End Sub
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Dim lrow As Long
    Dim rng As Range
    Dim var
    var = Right(Me.Caption, 1)
    With Sheets("Sheet" & var)
    lrow = Range("a" & Rows.Count).End(xlUp).Row
    Set rng = Range("A1:b" & lrow)
    rng.Sort key1:=Range("a1:a" & lrow), _
       order1:=xlAscending, Header:=xlNo
    End With
    End Sub
    enough from me for now

  22. #22
    Registered User
    Join Date
    09-18-2013
    Location
    ENgland
    MS-Off Ver
    Excel 2013
    Posts
    62

    Re: Help With Alphabetical Order In Listbox

    change your sheet item numbering to numbers not a formula?
    how would i go about doing that?

  23. #23
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    put a number in column a not a formula reading if column b is populated

  24. #24
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    Hope VBTrouble is still around

    Did some work on this and using only one userform and passing the search letter via a class module the list box list is now a bubble sorted array as domonic suggested

    bubble sort code mike erickson

    see attached
    Attached Files Attached Files
    Last edited by nigelog; 09-06-2019 at 12:12 PM.

  25. #25
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,283

    Re: Help With Alphabetical Order In Listbox

    @ nige

    You loaded all Commandbuttons from Findermain into the buttonsgroup where it should only be the alphabet buttons.

    Can you test example file for left errors (think it's error free now)
    Attached Files Attached Files
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  26. #26
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Help With Alphabetical Order In Listbox

    Hi Bakerman

    had just sorted that and the add new pattern code - posted workbook above but I'll be interested in testing your workbook

    Edit : seems same method of isolating non alphabetic buttons

    Ta Bakerman
    Last edited by nigelog; 09-06-2019 at 12:21 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to keep items in ListBox in alphabetical order when removing and adding items to box
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-31-2015, 07:13 AM
  2. Replies: 0
    Last Post: 02-15-2014, 10:30 AM
  3. Alphabetical order of files
    By LD in forum Excel General
    Replies: 1
    Last Post: 06-23-2006, 05:25 AM
  4. Alphabetical order possible? or not
    By OSSIE in forum Excel General
    Replies: 5
    Last Post: 06-22-2006, 02:25 PM
  5. why are my files out of alphabetical order?
    By kwardja in forum Excel General
    Replies: 2
    Last Post: 06-19-2006, 09:35 AM
  6. How do I put worksheets in alphabetical order
    By DLee in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 07-27-2005, 05:05 PM
  7. Alphabetical Order
    By Jennifer_Taylor in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-27-2005, 10:49 AM

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