Consulting

Results 1 to 4 of 4

Thread: Double click to bring up userform

  1. #1
    VBAX Regular
    Joined
    Dec 2005
    Posts
    17
    Location

    Double click to bring up userform

    Hi,

    I'm trying to bring up a userform with a double click in a range. The code below works but the focus stays in the cell in edit mode. Is there any way to set the focus on the userform with the cursor blinking inside the textbox? Also, I set the userform to be modeless, but it disappears everytime it is used. Is there any way to prevent this? I appreciate any help you might be able to give me.


    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Set player1 = Range("B5:B36")
    Set player2 = Range("D5:D36")
    Set player3 = Range("H5:H36")
    Set player4 = Range("J5:J36")
    For Each Target In Selection
        If Application.Intersect(Selection, player1) Is Nothing And _
        Application.Intersect(Selection, player2) Is Nothing And _
        Application.Intersect(Selection, player3) Is Nothing And _
        Application.Intersect(Selection, player4) Is Nothing Then
            MsgBox "Select Partnership Cell"
        Else
            UserForm1.Show
            UserForm1.???
       End If
    Next
    End Sub
    Last edited by geekgirlau; 01-02-2006 at 02:12 AM. Reason: Put line breaks in code

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by rgr
    Hi,

    I'm trying to bring up a userform with a double click in a range. The code below works but the focus stays in the cell in edit mode. Is there any way to set the focus on the userform with the cursor blinking inside the textbox? Also, I set the userform to be modeless, but it disappears everytime it is used. Is there any way to prevent this? I appreciate any help you might be able to give me.


    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Set player1 = Range("B5:B36")
    Set player2 = Range("D5:D36")
    Set player3 = Range("H5:H36")
    Set player4 = Range("J5:J36")
    For Each Target In Selection
        If Application.Intersect(Selection, player1) Is Nothing And Application.Intersect(Selection, player2) Is Nothing _
        And Application.Intersect(Selection, player3) Is Nothing And Application.Intersect(Selection, player4) Is Nothing Then
            MsgBox "Select Partnership Cell"
        Else
            UserForm1.Show
            UserForm1.???
        End If
    Next
    End Sub
    Basically, here's what you need to do
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    'to show modeless
    UserForm1.Show False
    'to get out of 'edit' mode
    Application.EditDirectlyInCell = False
    'set focus in text box
    UserForm1.TextBox1.SetFocus
    End Sub
    So, for your code, this gives
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Set player1 = Range("B5:B36")
    Set player2 = Range("D5:D36")
    Set player3 = Range("H5:H36")
    Set player4 = Range("J5:J36")
    For Each Target In Selection
        If Application.Intersect(Selection, player1) Is Nothing And Application.Intersect(Selection, player2) Is Nothing _
        And Application.Intersect(Selection, player3) Is Nothing And Application.Intersect(Selection, player4) Is Nothing Then
            MsgBox "Select Partnership Cell"
        Else
            UserForm1.Show False
            Application.EditDirectlyInCell = False
            UserForm1.TextBox1.SetFocus
        End If
    Next
    End Sub
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    VBAX Regular
    Joined
    Dec 2005
    Posts
    17
    Location
    That works very well, thank you Johnske, and Happy New Year

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Not a prob, and a Happy New Year to you also
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •