Results 1 to 5 of 5

Thread: [RESOLVED] Excel worksheet listbox selection to trigger VBA code

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    12

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3
    Member
    Join Date
    May 2008
    Posts
    36

    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

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    12

    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
  •  



Click Here to Expand Forum to Full Width