Consulting

Results 1 to 6 of 6

Thread: Clear activex listbox selection

  1. #1
    VBAX Newbie
    Joined
    May 2016
    Posts
    5
    Location

    Clear activex listbox selection

    Hello!

    I have an activeX listbox (single select) on a worksheet in excel. Listbox_click is used to run a macro vba driven report based upon the selection made. The report generates wonderfully ... however, the listbox selection remains in place. Where this becomes a problem is the next time that the workbook is opened. The previous selection remains and the report automatically generates. To solve this, I am trying to find out how to reset the listbox after the report completes running. I have tried to use this after the report macro completes running:

    For x = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(x) = True Then
    ListBox1.Selected(x) = False
    End If

    however, this did not really work - the report tries to run again right away, and a custom fail message is delivered telling the user that the report is already generated.

    I don't really need to fancy things up by creating a userForm. Its just a simple box to run a report on data from another spreadsheet in the workbook.

    Any ideas would be welcome!
    Thank you!

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Use Listbox_change() instead

  3. #3
    VBAX Newbie
    Joined
    May 2016
    Posts
    5
    Location
    Hello!

    I tried switching the code to ListBox_change from ListBox_Click. Unfortunately the same thing happens when I close out of the workbook and then open it again. The last selected report type which was chosen runs again automatically. I don't know if it has anything to do with another aspect: every time I open the workbook, I must select the option to enable the macros. Do I have the wrong settings or need different permissions? Any other ideas on changing ListBox?

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.EnableEvents = False
    Sheets(1).ListBox1.ListIndex = -1
    Application.EnableEvents = True
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Please post your workbook.

  6. #6
    VBAX Newbie
    Joined
    May 2016
    Posts
    5
    Location

    THANK YOU!!!

    Quote Originally Posted by mdmackillop View Post
    Try
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Application.EnableEvents = False
     Sheets(1).ListBox1.ListIndex = -1
     Application.EnableEvents = True
     End Sub
    Thank you so much! This does the trick! I truly appreciate your help

Tags for this Thread

Posting Permissions

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