+ Reply to Thread
Results 1 to 7 of 7

Live update of .ListBox.Selected

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Live update of .ListBox.Selected

    Hi,

    I have a userform that dynamically hides and unhides columns based on the selections in a listbox. .Selected(0) is "Show All" and the following list items refer to the other columns. I would like to automatically deselect "Show All" when any other list item is selected. Adding .Selected(0) = False into the With nest obviously sends it into a loop and crashes Excel. I know it's simple but I'm struggling to figure it out.

    Private Sub ListBox1_Change()
        Dim Ndx As Long
        Application.ScreenUpdating = False
        With Me.ListBox1
                If .Selected(0) Then
                ActiveSheet.Columns("L:BM").Hidden = False
            Else
                For Ndx = 1 To .ListCount - 1
                    Sheets("Analysis").Columns(Ndx + 11).Hidden = Not .Selected(Ndx)
                Next Ndx
            End If
        End With
        Application.ScreenUpdating = True
    End Sub
    Thanks in anticipation

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Live update of .ListBox.Selected

    Hello radddogg,

    You should use a CommandButton to run the macro. This will also let the user edit the selections before they are applied.

    Here is an example where the code has been moved to a command button named "ComanndButton1" on the UserForm.
    Private Sub CommandButton1_Click()
        Dim Ndx As Long
        Application.ScreenUpdating = False
        With Me.ListBox1
                If .Selected(0) Then
                ActiveSheet.Columns("L:BM").Hidden = False
            Else
                For Ndx = 1 To .ListCount - 1
                    Sheets("Analysis").Columns(Ndx + 11).Hidden = Not .Selected(Ndx)
                Next Ndx
            End If
        End With
        Application.ScreenUpdating = True
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Live update of .ListBox.Selected

    Quote Originally Posted by Leith Ross View Post
    Hello radddogg,

    You should use a CommandButton to run the macro. This will also let the user edit the selections before they are applied.

    Here is an example where the code has been moved to a command button named "ComanndButton1" on the UserForm.
    Private Sub CommandButton1_Click()
        Dim Ndx As Long
        Application.ScreenUpdating = False
        With Me.ListBox1
                If .Selected(0) Then
                ActiveSheet.Columns("L:BM").Hidden = False
            Else
                For Ndx = 1 To .ListCount - 1
                    Sheets("Analysis").Columns(Ndx + 11).Hidden = Not .Selected(Ndx)
                Next Ndx
            End If
        End With
        Application.ScreenUpdating = True
    End Sub
    Hi,

    It previously was on a command button but I wanted to simplify the form and make it more intuitive. The current effect works very well. I just wanted to remove the need for the user to have to deselect "Show All" before their selections take place, cheers.

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Live update of .ListBox.Selected

    Hi,

    I tend to agree about using a button but purely for information you can use a control variable. Add to the top of the form module
    Dim bNoEvents as Boolean
    and then amend your code to
    Private Sub ListBox1_Change()
        Dim Ndx As Long
        If bNoEvents Then Exit Sub
        Application.ScreenUpdating = False
        With Me.ListBox1
                If .Selected(0) Then
                ActiveSheet.Columns("L:BM").Hidden = False
            Else
                bNoEvents = True
                .Selected(0) = False
                bNoEvents = False
                For Ndx = 1 To .ListCount - 1
                    Sheets("Analysis").Columns(Ndx + 11).Hidden = Not .Selected(Ndx)
                Next Ndx
            End If
        End With
        Application.ScreenUpdating = True
    End Sub
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Live update of .ListBox.Selected

    Thanks for the suggestion, I went with Alpha's solution in the end, cheers

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Live update of .ListBox.Selected

    This deselects all other selections when you select All and deselects All when you select any column.

    Private Sub ListBox1_Change()
        Dim Ndx As Long
        Static DisableListBoxEvents As Boolean
        If DisableListBoxEvents = True Then Exit Sub
        DisableListBoxEvents = True
        Application.ScreenUpdating = False
        With Me.ListBox1
            If .ListIndex = 0 And .Selected(0) Then
                ActiveSheet.Columns("L:BM").Hidden = False
                For Ndx = 1 To .ListCount - 1
                    .Selected(Ndx) = False
                Next Ndx
            Else
                .Selected(0) = False
                For Ndx = 1 To .ListCount - 1
                    Sheets("Analysis").Columns(Ndx + 11).Hidden = Not .Selected(Ndx)
                Next Ndx
            End If
        End With
        DisableListBoxEvents = False
        Application.ScreenUpdating = True
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  7. #7
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Live update of .ListBox.Selected

    Hats off to you Alpha. This is exactly what I was looking for. Thank you for not only solving my problem but also helping me increase by knowledge

    +rep

+ 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. Show dynamic values at listbox depending value selected another listbox
    By Judith_Chao in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2017, 08:30 AM
  2. Populate (ca 200) Txtboxes depending on listbox selection, live-filter for listbox &1 more
    By InternInNeed in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-14-2016, 09:56 AM
  3. Live update between 2 different spreadsheets
    By misterjam in forum Excel General
    Replies: 1
    Last Post: 11-18-2015, 05:29 PM
  4. [SOLVED] Update (strikethrough) Listbox item selected within sheet
    By ShaunRoos in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-03-2014, 02:45 PM
  5. Copy Selected items from multicolumn, multiselect listbox to another listbox
    By Willigb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2013, 11:27 AM
  6. Live Update results to web
    By mjcloward in forum Access Tables & Databases
    Replies: 2
    Last Post: 07-26-2012, 10:50 AM
  7. Live update with excel?
    By Zirus in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-27-2007, 12:17 PM

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