VBA UserForm listbox selection value to true on key press (enter)

Mowgy

New Member
Joined
Apr 5, 2013
Messages
9
Hi there,

I currently found on this board a code to activate optionbuttons or checkboxes via the enter key on a userform.

This is done through a class module.

(The post I used)

I would like to do the same with the listboxes I have.

I have tried doing exactly the same as above but I don't understand the code so not sure how to modify it to work...

I suppose this has to do with the fact that Listboxes are not simple yes/no but depending on the listindex ...

If anyone can help I would be eternally grateful.

Mowgy





Class Module code (renamed properly):
Code:
Public WithEvents ListBoxGroup As MSForms.ListBox

Private Sub ListBoxGroup_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then Me.ListBoxGroup.Value = Not Me.ListBoxGroup.Value
End Sub

Code in my userform

Code:
Dim Lists() As New ListBoxClass
Code:
Private Sub UserForm_Initialize()    
    Dim Ctrl As Control
    Dim Count As Integer
    Dim Count2 As Integer
    Dim Count3 As Integer
    
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "OptionButton" Then
            Count = Count + 1
            ReDim Preserve Buttons(1 To Count)
            Set Buttons(Count).OptionButtonGroup = Ctrl
        ElseIf TypeName(Ctrl) = "CheckBox" Then
            Count2 = Count2 + 1
            ReDim Preserve Boxes(1 To Count2)
            Set Boxes(Count2).CheckBoxGroup = Ctrl
        ElseIf TypeName(Ctrl) = "ListBox" Then
            Count3 = Count3 + 1
            ReDim Preserve Lists(1 To Count)
            Set Lists(Count3).ListBoxGroup = Ctrl
        End If
    Next
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Welcome to MrExcel.

What is it that you want the Enter key to do exactly? If you move up or down in a listbox the item under the cursor is selected.
 
Upvote 0
Welcome to MrExcel.

What is it that you want the Enter key to do exactly? If you move up or down in a listbox the item under the cursor is selected.


Hi Andrew,

Thank you for the welcome.

I would like it to turn tick yes on the listbox.

I have the list box set up as a accepting multiple selections so each time one would press enter it would turn that selection to a True value. (I have it formatted as a combo box with the square tick boxes).

Similar to what the option buttons and checkboxes are doing with the code, if I tab to one, then press enter the value is turned to true / false.

Does that explain what I am trying to do ?

Cheers,

Mowgy
 
Upvote 0
Try:

Code:
'Class module named ListBoxClass
Public WithEvents ListBoxGroup As MSForms.ListBox

Private Sub ListBoxGroup_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 13 Then
        With Me.ListBoxGroup
            .Selected(.ListIndex) = Not .Selected(.ListIndex)
        End With
    End If
End Sub

'UserForm module


Dim Lists() As New ListBoxClass
Private Sub UserForm_Initialize()
    Dim Ctrl As Control
    Dim Count As Integer
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "ListBox" Then
            Count = Count + 1
            ReDim Preserve Lists(1 To Count)
            Set Lists(Count).ListBoxGroup = Ctrl
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top