[RESOLVED] Excel worksheet listbox selection to trigger VBA code
I have an excel list box working fine with the LOOKUP function to populate the required cells. What I am trying to do is if the first item is selected
"00.User defined"
a form will open to allow the user to enter the data required.
I have managed to get this working using the Worksheet_SelectionChange command. But using this function the command only runs after the focus has left the listbox cell (G3).
My question is;
Is there a way to trigger the VBA code (to open the form) when "00.User defined" is selected from the listbox in the excel worksheet (while the cell containing the listbox still has the focus)?
Thanks for any help you can give me.
Re: Excel worksheet listbox selection to trigger VBA code
what type of listbox are you using? a forms.listbox, if so put code in the listbox click event
Re: Excel worksheet listbox selection to trigger VBA code
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
‘Set range to the cell you want to check for specific text
‘MACRO is the code you want ran when G3 = 00.user.defined
'OTHER_MACRO is the code you want ran when G3 <> 00.user.defined
If Range("G3").Value = "00.user.defined" Then
MACRO
Else: OTHER_MACRO
End If
End Sub
Re: Excel worksheet listbox selection to trigger VBA code
Use Worksheet_Change() instead of Worksheet_SelectionChange() as Hoop suggested.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'-- Target may be a range of one cell or more than one cells
' check that G3 is a cell within Target range
If Not Intersect(Target, Me.Range("G3")) Is Nothing Then
'-- G3 is a cell within Target, now check G3.Value
If Me.Range("G3") = "00.User defined" Then
'-- Your code here. This code is only triggered
' when G3 = "00.User defined"
MsgBox "G3 has been changed to '00.User defined'"
End If
End If
End Sub
Re: Excel worksheet listbox selection to trigger VBA code
Thanks for the help. I managed to get it to work using an ActiveX combobox with then linked to an cell on the worksheet, so populating the required cells from there.