Show/Hide overlapping buttons

kanadaaa

Active Member
Joined
Dec 29, 2019
Messages
348
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a question about the behaviour of buttons.
I have CommandButton1 and CommandButton2 on sheet1 and they overlap in position.
What I want to do is hide CB1 and show CB2 when CB1 is clicked, and hide CB2 and show CB1 when CB2 is clicked.
Then I tried this code:
VBA Code:
Private Sub CommandButton1_Click()
     CommandButton1.Visible = False
     CommandButton2.Visible = True
End Sub

Private Sub CommandButton2_Click()
     CommandButton2.Visible = False
     CommandButton1.Visible = True
End Sub
It works quite well, but I'm not fully satisfied because when I keep pressing the mouse button, the invisible button "flashes", or becomes visible for an instant (when I release the mouse button the visible button becomes invisible and the invisible button becomes visible, just in the way I want the code to work).
I don't want it to "flash" on click. How could I solve this problem? Thank you.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I can't reproduce the flash behavior you describe, but you might try this:
VBA Code:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
CommandButton1.Visible = False
CommandButton2.Visible = True
Application.ScreenUpdating = True
End Sub
...and similarly with the other button.
 
Upvote 0
Just out of curiosity why are you doing this? And if the buttons are not exactly same size/position they could flash...
 
Upvote 0
Thank you two for your replies.
Just out of curiosity why are you doing this? And if the buttons are not exactly same size/position they could flash...
I use them to toggle Auto Filter On/Off, one for turning it on and the other for turning it off. I wanted to hide one because you never need both of them.

Anyway, I thought I'd just use one button and change its function and caption on click.
The following code resolved the issue, but I don't know why my button flashed.
VBA Code:
Private Sub CommandButton2_Click()
    Dim msb As Long
    If ActiveSheet.Cells(5, 1) = "" Then
       msb = MsgBox("There is no reference data.", vbExclamation, "Error")
    ElseIf CommandButton2.Caption = "Auto Filter On" Then
        FilterForm.Show
        CommandButton2.Caption = "Auto Filter Off"
    ElseIf CommandButton2.Caption = "Auto Filter Off" Then
        Call UndoAutoFilter
        CommandButton2.Caption = "Auto Filter On"
    End If
End Sub
VBA Code:
Sub UndoAutoFilter()
    Dim rc As Long
    If ActiveSheet.AutoFilterMode Then
        With ActiveSheet
            .Unprotect Password:="1234"
            .AutoFilterMode = False
            .Protect Password:="1234", UserInterfaceOnly:=True
        End With
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top