How to Add a Hover Effect for Buttons in VBA UserForms
Introduction
In this tutorial, I’ll guide you through adding hover effects to your buttons in VBA UserForms. Unfortunately, VBA does not directly support hover effects, such as changing the button’s background color when the mouse hovers over it. However, there are workarounds to achieve this.
Two methods for adding a hover effect
The first method involves using class modules. The code snippet for the classes can be found on this blog: Create Button Hover Effect, which allows you to implement hover effects on all buttons. The second method utilizes images as buttons.
The second method is by using images. We can leverage their inherent ‘MouseOver’ and ‘Click’ effects to mimic button behavior. I’ll show you how to set this up and make it work effectively in the video. Use the VBA code provided below to mimic the hover effects with images. Additionally, you can download the mouse cursor icon used in this tutorial by clicking the button below:
Code Snippet for hover effect when using images
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) ' Reset the buttons to their default state when the mouse is not hovering over them btnEdit.Visible = True End Sub Private Sub btnEdit_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) ' Show hover image when mouse moves over btnEdit btnEdit.Visible = False btnEditHover.Visible = True End Sub Private Sub btnEditHover_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) ' Keep hover image visible when mouse is over btnEditHover btnEdit.Visible = False btnEditHover.Visible = True End Sub Private Sub btnEditHover_Click() ' Action to perform when the hover image button is clicked MsgBox "Edit button clicked!" End Sub