Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 24

Thread: double click on listbox

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location

    double click on listbox

    [VBA]Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    'Check for range addresses
    If ListBox1.ListCount = 0 Then Exit Sub
    'GoTo doubled clicked address
    Application.Goto Range(ListBox1.Text), True
    End Sub[/VBA]

    This code is for: when you double click in cell address which appears in list box it selects the row in which is the record, but when record is on other sheet it does not show that sheet neither does select the row how do i do that

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi saban,

    I haven't tested this at all, but how about:
    [vba]Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    'Check for range addresses
    If ListBox1.ListCount = 0 Then Exit Sub
    'GoTo doubled clicked address
    Range(ListBox1.Text).Parent.Activate
    Application.Goto Range(ListBox1.Text), True
    End Sub[/vba]
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    no it doesnt work it is still the same

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Saban,

    What is the value of Listbox1.Text when it won't work? Is it a named range, a cell address ($A$1), a fully qualified address (Sheet2!$A$1)...
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    just $A$1 I guess it should be fully qualified

    Thnx for your help

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Yes, will definately need to be fully qualified. It sounds like it's doing what you told it to and not what you want it to. LOL!

    If you need any more assistance with it, don't heistate to post back.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    From what I understand, Listbox1.Text will not return you the doubleclicked item. You would need to loop through all the items and get the selected item. This works for me ..

    [vba]Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Dim i As Long, Pos As Long
    Dim strSheet As String, strAddy As String
    With Me.ListBox1
    For i = 0 To .ListCount - 1
    If .Selected(i) Then Pos = i: Exit For
    Next i
    If i = .ListCount Then Exit Sub
    strSheet = Left(.List(i), InStr(1, .List(i), "!") - 1)
    strAddy = Right(.List(i), Len(.List(i)) - Len(strSheet) - 1)
    Sheets(strSheet).Activate
    Range(strAddy).Activate
    End With
    Unload Me
    End Sub[/vba]

    I had my listbox populated as such ..

    [vba]Private Sub UserForm_Initialize()
    Dim i As Long
    For i = 1 To Worksheets.Count
    With Me.ListBox1
    .AddItem Sheets(i).Name & "!A1"
    End With
    Next i
    Me.ListBox1.ListIndex = 0
    End Sub[/vba]

    HTH

  8. #8
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    you are right i hate this to

    One stupid question how do i fully qualify it

    Thnx for all your time and patience
    saban

  9. #9
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    i get error invalid outside call or procedure when inserting your code

  10. #10
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    ups sory i didnt populate list like you
    I will try
    thnx

  11. #11
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    I need first to do the serach and then populate listbox does this change anything
    because when i put your code in I already have list box filled before i do the search

  12. #12
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    It depends on how you populate the listbox. I just populated it with values such as...

    Sheet1!A1
    Sheet2!A1
    Sheet3!A1
    ...

    I used the ! character as a seperator to parse out the sheet and the address.

  13. #13
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Zack

    The double click will return the value from the listbox.

    This worked for me.
    [vba]Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Dim addy As String
    addy = ListBox1.Text
    Unload Me
    Application.Goto Range(addy)
    End Sub

    Private Sub UserForm_Initialize()
    Dim i As Long
    For i = 1 To Worksheets.Count
    With Me.ListBox1
    .AddItem Sheets(i).Name & "!A1"
    End With
    Next i
    Me.ListBox1.ListIndex = 0
    End Sub[/vba]

  14. #14
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    thnx guys for all your help i will try and let you know

    saban

  15. #15
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    [VBA] Application.Goto Range(addy) [/VBA] i get this error global failed here
    any ideas why

  16. #16
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What value does addy have when you get the error?

    Can you attach a sample workbook?

  17. #17
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by saban
    [VBA]Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    'Check for range addresses
    If ListBox1.ListCount = 0 Then Exit Sub
    'GoTo doubled clicked address
    Application.Goto Range(ListBox1.Text), True
    End Sub[/VBA]

    This code is for: when you double click in cell address which appears in list box it selects the row in which is the record, but when record is on other sheet it does not show that sheet neither does select the row how do i do that
    [vba]Option Explicit

    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Application.Goto Range(ListBox1).EntireRow
    Unload Me
    End Sub

    Private Sub UserForm_Activate()
    Dim N As Long
    For N = 1 To Worksheets.Count
    ListBox1.AddItem Sheets(N).Name & "!A1"
    Next
    End Sub[/vba]Should work ok...
    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.

  18. #18
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    Another usual suspect for an error is space in the sheet name.
    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    
        Application.Goto Range(ListBox1.List(ListBox1.ListIndex))
        
    End Sub
    Private Sub UserForm_Initialize()
        Dim shttemp As Worksheet
        
        For Each shttemp In ActiveWorkbook.Worksheets
            ListBox1.AddItem "'" & shttemp.Name & "'!A1"
        Next
    End Sub
    Cheers
    Andy

  19. #19
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    guys i need to double click in listbox when search is done not before
    when i double click when userform initialize it works fine but it is not to much help for me first i need to do the search then i get results together with cell address and then i must double click on cell address and it should activate sheet that this address is in

    any ideas

  20. #20
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What search are you referring to?

    I don't see any searching going on in your posted code.

Posting Permissions

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