Avatar of Mike French
Mike French
Flag 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

Microsoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Mike French

8/22/2022 - Mon
Martin Liss

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

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

Martin Liss

That looks like your original code rather than what I posted in my previous post. Did you actually use my code?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Mike French

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Martin Liss

Just for my benefit, does this work the way you want?
29167723.xlsm
Mike French

ASKER
Martin,

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

Thanks,
Martin Liss

"make it work" implies to me that you had to change the code. Is that correct?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mike French

ASKER
I had to put the "On error resume next" into it because i kept getting errors when i hovered over it.