Search code examples
excelvbalistboxactivex

Excel Activex Listbox to open and close on selection of same cell without needing to click out onto another cell first


This code shows ListBox1 when cell A2 is clicked, hides it when A2 is clicked a second time or ListBox1 is no longer selected. The selections are output to A2.

The problem is that after A2 is clicked once to open and once to close, you must click another cell before clicking on A2 again to create the perceived toggle effect of the ListBox.

I have tried repeating Application.EnableEvents = False [A3].Select Application.EnableEvents = True just before End If however when trying to select any other cell on the sheet, only A3 is selected.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With ActiveSheet.ListBox1
        If Target(1).Address = "$A$2" And .Visible = False Then
            .Visible = True
            Application.EnableEvents = False
            [A3].Select
            Application.EnableEvents = True
        Else
            .Visible = False
            For I = 0 To .ListCount - 1
                If .Selected(I) Then txt = txt & ", " & .List(I)
            Next
            [A2] = Mid(txt, 2)  'remove first comma and output to A2 cell
        End If
    End With
End Sub

Question Updated to include the answer provided, while incorporating part of the code from above to output the ListBox selections to cell A2. The new problem is that the selections already made, continue to multiply in cell A2 everytime the ListBox is closed, rather than only adding new selections.

Option Explicit

Dim SelectCell As Boolean
Dim i As Long
Dim txt As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With ActiveSheet.ListBox1
        If Target(1).Address = "$A$2" And .Visible = False Then
            .Visible = True
            Application.EnableEvents = False
            [A3].Select
            Application.EnableEvents = True
            SelectCell = True
        Else
            .Visible = False
            
            For i = 0 To .ListCount - 1
                If .Selected(i) Then txt = txt & ", " & .List(i)
            Next
            [A2] = Mid(txt, 2)  'remove first comma and output to A2 cell
            
            
            If SelectCell = True Then
                Application.EnableEvents = False
                [A3].Select
                Application.EnableEvents = True
                SelectCell = False
            End If
        End If
    End With
End Sub

Solution

  • Yes this works however when you click any other cell in the sheet it keeps selecting cell A3. I will clarify question. Thanks – aye cee 1 min ago

    Is this what you are trying?

    Option Explicit
    
    Dim SelectCell As Boolean
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        With ActiveSheet.ListBox1
            If Target(1).Address = "$A$2" And .Visible = False Then
                .Visible = True
                Application.EnableEvents = False
                [A3].Select
                Application.EnableEvents = True
                SelectCell = True
            Else
                .Visible = False
                If SelectCell = True Then
                    Application.EnableEvents = False
                    [A3].Select
                    Application.EnableEvents = True
                    SelectCell = False
                End If
            End If
        End With
    End Sub
    

    The Listbox should open and close endless times by clicking A2 or close by clicking outside, while also allowing selection of other cells on the sheet. Yes I included all code. On your end can you repeatedly click A2 to open close without selecting any other cell? – aye cee 2 mins ago

    It does exactly that. See this

    enter image description here

    Alternate solution

    Use Worksheet_BeforeDoubleClick with Worksheet_SelectionChange. See this. Now the ListBox1 will show everytime you double click on A2 and hide when any other cell is selected.

    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        If Target(1).Address = "$A$2" And ListBox1.Visible = False Then
            ListBox1.Visible = True
            Cancel = True
        Else
            ListBox1.Visible = False
        End If
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If ListBox1.Visible = True Then ListBox1.Visible = False
    End Sub