Link to home
Start Free TrialLog in
Avatar of Mike French
Mike FrenchFlag for United States of America

asked on

Mouse down event to select item in combo box not working correctly

I am using an ActiveX combo box on a worksheet. I am trying to select an item with a mouse. I have found the code below and it does work. However, unless you place the mouse pointer on the very bottom of the item highlighted in blue, it will select the item above it. Is there another way to do this? or is there a way to adjust the code to make this function better?

Private Sub TempCombo_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
  
   If TempCombo.TopIndex > -1 Then
        Dim curIndex As Integer
        curIndex = TempCombo.TopIndex + Application.WorksheetFunction.RoundDown(y / 13.5, 0)
        CurValue = TempCombo.list(curIndex)
   End If
  
End Sub

Open in new window

Avatar of Martin Liss
Martin Liss
Flag of United States of America image

This will select the next entry in the combobox.

Private Sub TempCombo_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)

    With TempCombo
        If .ListIndex > -1 Then
            .ListIndex = .ListIndex + Application.WorksheetFunction.RoundDown(y / 13.5, 0)
        End If
   End With
  
End Sub

Open in new window

Avatar of Mike French

ASKER

Well, I tried using your solution but I get no value whatsoever into my variable. See the code below. You essentially changed the "TopIndex" for "ListIndex". What am I missing here?

    If TempCombo.ListIndex > -1 Then
        Dim CurIndex As Integer
        CurIndex = TempCombo.ListIndex + Application.WorksheetFunction.RoundDown(y / 13.5, 0)
        CurValue = TempCombo.list(CurIndex)
    End If

Open in new window

That looks like your original code rather than what I posted in my previous post. Did you actually use my code?
ASKER CERTIFIED SOLUTION
Avatar of Mike French
Mike French
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just for my benefit, does this work the way you want?
29167723.xlsm
Martin,

I tried your worksheet and I can make it work as well!

Thanks,
"make it work" implies to me that you had to change the code. Is that correct?
I had to put the "On error resume next" into it because i kept getting errors when i hovered over it.