|
-
Jun 20th, 2008, 12:20 PM
#1
Thread Starter
New Member
[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.
-
Jun 21st, 2008, 06:51 AM
#2
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 21st, 2008, 08:49 AM
#3
Member
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
-
Jun 22nd, 2008, 06:16 PM
#4
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
-
Jun 23rd, 2008, 06:22 AM
#5
Thread Starter
New Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|