+ Reply to Thread
Results 1 to 4 of 4

Next loop is not working

Hybrid View

  1. #1
    Registered User
    Join Date
    01-30-2019
    Location
    Mexico
    MS-Off Ver
    2007
    Posts
    3

    Next loop is not working

    Hi everyone:

    I'm new in this forum and I would like to know if you can help me with this issue:

    My code is this:

    Private Sub ListBox1_Change()
    Dim a As Long


    If ListBox1.Selected(0) = True Then
    For a = 1 To listobox1.ListCount
    ListBox1.Selected(a) = True
    Next
    End If

    End Sub

    What I'm trying to do is a select all / deselect all checkbox inside listbox, I already create the checkbox and call the lines of code from the listbox change method, the trouble I have is when a = 1 check the next item in the list but instead to advance to the next a making its value 2, what it does is jumping back to the name of the sub routine. Making and endless loop.

    Any idea what is wrong?

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Next loop is not working

    You'll want to disable the Event trigger once you've set it off, and then turn it back on when you're done.
    Private Sub ListBox1_Change()
    Dim a As Long
    
    If ListBox1.Selected(0) = True Then
        Application.EnableEvents = False
        For a = 1 To listobox1.ListCount
            ListBox1.Selected(a) = True
        Next
    End If
    
    Application.EnableEvents = True
    End Sub
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Next loop is not working

    Assuming listbox is on a userform

    Option Explicit
    
    Private mUpdatingList As Boolean
    
    Private Sub UserForm_Initialize()
    
        mUpdatingList = True
        With ListBox1
            .MultiSelect = fmMultiSelectMulti
            .ListStyle = fmListStyleOption
            .List = Array("Select All", 1, 2, 3, 4, 5)
        End With
        mUpdatingList = False
        
    End Sub
    
    
    Private Sub ListBox1_Change()
        Dim a As Long
        Dim state As Long
        
        If mUpdatingList Then Exit Sub
        
        If ListBox1.ListIndex = 0 Then
            mUpdatingList = True
            state = ListBox1.Selected(0)
            For a = 1 To ListBox1.ListCount - 1
                ListBox1.Selected(a) = state
            Next
            If state Then
                ListBox1.List(0) = "Deselect All"
            Else
                ListBox1.List(0) = "Select All"
            End If
            mUpdatingList = False
        End If
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  4. #4
    Registered User
    Join Date
    01-30-2019
    Location
    Mexico
    MS-Off Ver
    2007
    Posts
    3

    Re: Next loop is not working

    Thanks a lot Andy, now its working, and also great the captions select all and deselect all.
    I'm not sure why -1 in the state allows me to change the checkbox selection, but I will study with more attention.

    Regards

+ 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] Do Until Loop Not Working
    By jjl287 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-30-2015, 04:49 PM
  2. For loop within loop not working correctly
    By HalPlz in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-25-2015, 12:06 AM
  3. [SOLVED] For each loop not working
    By schzuki in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-31-2014, 09:54 AM
  4. [SOLVED] For Next Loop not working
    By jacob@thepenpoint in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-12-2013, 02:24 PM
  5. [SOLVED] Do while loop not working
    By rikkyshh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-09-2013, 01:47 PM
  6. [SOLVED] Loop not working!!
    By Simon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2005, 12:05 PM
  7. Do...Loop not working
    By Sunny Lin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2005, 09:06 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