+ Reply to Thread
Results 1 to 7 of 7

Scroll using the Mouse Wheel in a ListBox

Hybrid View

  1. #1
    Registered User
    Join Date
    11-20-2020
    Location
    Québec, Canada
    MS-Off Ver
    MS Office 2013
    Posts
    19

    Scroll using the Mouse Wheel in a ListBox

    Hi!

    I'm a total newbie when it comes to API. I have a pretty long ListBox in my UserForm and I would like to be able to use the mouse wheel to scroll through it. Trouble is I'm using a 64bit version of Excel and I can't find what I need to change from the 32bit code I found. Here's the code:

    In a module:
    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
       (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
          (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    'To be able to scroll with mouse wheel within Userform
    
    Private Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" ( _
        ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, _
        ByVal lParam As Long) As Long
    
    
    Private Const GWL_WNDPROC = -4
    Private Const WM_MOUSEWHEEL = &H20A
    
    Dim LocalHwnd As Long
    Dim LocalPrevWndProc As Long
    Dim myForm As UserForm
    
    Private Function WindowProc(ByVal Lwnd As Long, ByVal Lmsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        'To handle mouse events
        Dim MouseKeys As Long
        Dim Rotation As Long
        
        If Lmsg = WM_MOUSEWHEEL Then
            MouseKeys = wParam And 65535
            Rotation = wParam / 65536
            'My Form s MouseWheel function
            UserForm1.MouseWheel Rotation
        End If
        WindowProc = CallWindowProc(LocalPrevWndProc, Lwnd, Lmsg, wParam, lParam)
    End Function
    
    Public Sub WheelHook(PassedForm As UserForm)
        'To get mouse events in userform
        On Error Resume Next
        
        Set myForm = PassedForm
        LocalHwnd = FindWindow("ThunderDFrame", myForm.Caption)
        LocalPrevWndProc = SetWindowLong(LocalHwnd, GWL_WNDPROC, AddressOf WindowProc)
    End Sub
    
    Public Sub WheelUnHook()
        'To Release Mouse events handling
        Dim WorkFlag As Long
        
        On Error Resume Next
        WorkFlag = SetWindowLong(LocalHwnd, GWL_WNDPROC, LocalPrevWndProc)
        Set myForm = Nothing
    End Sub
    In the UserForm:
    Option Explicit
    Private Sub UserForm_Activate()
        WheelHook Me 'For scrolling support
    End Sub
    
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    WheelUnHook     'For scrolling support
    '...
    End Sub
    
    Private Sub UserForm_Deactivate()
    WheelUnHook     'For scrolling support
    '...
    End Sub
    
    Public Sub MouseWheel(ByVal Rotation As Long)
    '************************************************
    ' To respond from MouseWheel event
    ' Scroll accordingly to direction
    '
    ' Made by:  Mathieu Plante
    ' Date:     July 2004
    '************************************************
    If Rotation > 0 Then
        'Scroll up
        If ListBox1.TopIndex > 0 Then
            If ListBox1.TopIndex > 3 Then
                ListBox1.TopIndex = ListBox1.TopIndex - 3
            Else
                ListBox1.TopIndex = 0
            End If
        End If
    Else
        'Scroll down
        ListBox1.TopIndex = ListBox1.TopIndex + 3
    End If
    End Sub
    Now, when I run this as is, I get a compilation error. I need to change these:

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
       (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
          (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    'To be able to scroll with mouse wheel within Userform
    
    Private Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" ( _
        ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, _
        ByVal lParam As Long) As Long
    to Declare PtrSafe. I did and now when I run it, I get a type incompatibility error with at least this sub

    Public Sub WheelHook(PassedForm As UserForm)
        'To get mouse events in userform
        On Error Resume Next
        
        Set myForm = PassedForm
        LocalHwnd = FindWindow("ThunderDFrame", myForm.Caption)
        LocalPrevWndProc = SetWindowLong(LocalHwnd, GWL_WNDPROC, AddressOf WindowProc)
    End Sub
    What else do I have to change to make it work with a 64bit version of Excel? Best case scenario would be a code that runs on either version, but I will be very happy if it works on 64bit.

    Hope I'm clear enough. If you need more info, I'll be glad to clarify!

  2. #2
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: Scroll using the Mouse Wheel in a ListBox

    Why not use the MouseUp_Event ?
    Far faster than Scrolling then selecting.
    All done with one action - hold down left button - drag down list - selection made on release.
    Saves 'acres' of code.
    torachan.

  3. #3
    Registered User
    Join Date
    11-20-2020
    Location
    Québec, Canada
    MS-Off Ver
    MS Office 2013
    Posts
    19

    Re: Scroll using the Mouse Wheel in a ListBox

    I want the process to go by instinct for the user. For that I need to recreate the process the user is used to. I don't mind a bit of coding. It might not be ideal but since I know it can be done with a 32bit version, there must be a way to do it with a 64bit version, right?

  4. #4
    Registered User
    Join Date
    11-20-2020
    Location
    Québec, Canada
    MS-Off Ver
    MS Office 2013
    Posts
    19

    Re: Scroll using the Mouse Wheel in a ListBox

    Hi again! Has anyone found a way to make this work?

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Scroll using the Mouse Wheel in a ListBox

    Have you searched this forum? I know this question has come up a few times before. (Hint: check the similar threads list at the bottom of the page)
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Scroll using the Mouse Wheel in a ListBox

    Play around with this...Should work with both 32 & 64 Bit

    Or why not just incorporate a "Type as You Go Filter" to narrow listbox selections....
    Attached Files Attached Files
    Last edited by Sintek; 12-09-2020 at 08:25 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  7. #7
    Registered User
    Join Date
    11-20-2020
    Location
    Québec, Canada
    MS-Off Ver
    MS Office 2013
    Posts
    19

    Re: Scroll using the Mouse Wheel in a ListBox

    Quote Originally Posted by rorya View Post
    Have you searched this forum? I know this question has come up a few times before. (Hint: check the similar threads list at the bottom of the page)
    I did search for this but I always ended up with 32bit solutions... And I apparently couldn't find the other threads that I now find in the similar threads . Anyway, I see them now, guess I'll check them out, along with this ↓

    Quote Originally Posted by sintek View Post
    Play around with this...Should work with both 32 & 64 Bit

    Or why not just incorporate a "Type as You Go Filter" to narrow listbox selections....
    Sintek, thanks for the reply, I did incorporate the filter, it works flawlessy but I figured, if for some reason the user's list is shorter and they know their list pretty well they might want to scroll through it to find what they need. As I said to torachan, I want the process to go by instinct, and for that I need to think of anything a user might try to do. Right now the scroll and the filter are pretty much the only features I can think of that the user would use.

    Anyway, I will start playing around the bit of code you sent me. I think that might do what I need if I can tweak it a little. All in all, it's the closest to what I want to do, the only thing I need to figure out is how to avoid any selection when scrolling (I only need the view to change not change selection with the scroll). Any clue how to do it (if this can be done at all)?

    I'll keep digging on my side and update the thread if I find anything relevant

    Cheers!!

    Edit #1: Alright, I found what I need right here in /excel-programming-vba-macros/1249343-mouse-scroll-wheel-in-64-bit.html but I'm completely lost. I'll contact the programmer to see if he can explain a little bit
    Last edited by nightseeker; 12-10-2020 at 06:07 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Mouse Scroll Wheel in 64 bit
    By Soulfien in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-16-2021, 09:04 AM
  2. [SOLVED] Mouse Scroll Wheel in 64 bit help needed
    By Soulfien in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-13-2019, 09:19 PM
  3. Userform Listbox Move up/down using the Mouse Wheel Scroll
    By alexgoaga in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-07-2018, 08:08 AM
  4. Controlling Scroll in Listbox with Mouse wheel
    By bagullo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-09-2014, 09:05 AM
  5. Scroll with mouse wheel on userform
    By Josh_123456 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-08-2007, 08:19 PM
  6. scroll with wheel mouse
    By Scott in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-11-2005, 04:05 PM
  7. [SOLVED] scroll listbox with mouse wheel
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2005, 05:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1