Form Button Click

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,428
Here is my code that runs then the user clicks a button on the form.

Code:
Sub Toggle(Button)
With Button
    If .BackColor = &H8000000F Then 'if its gray
            .BackColor = &H80FF80 'turn green
            .Font.Bold = True
            .Font.Size = 70
            .Left = .Left - 25
            .Width = .Width + 50
            .Top = .Top - 25
            .Height = .Height + 50
            
        ElseIf .Font.Bold = True Then .Font.Underline = True
            
        Else 'turn back to gray
            .BackColor = &H8000000F
            .Font.Bold = False
            .Font.Size = 30
             .Left = .Left + 25
            .Width = .Width - 50
            .Top = .Top + 25
            .Height = .Height - 50
            .Font.Underline = False
    End If
End With
End Sub


Private Sub CommandButton1_Click()
    Call Toggle(CommandButton1)
End Sub

When the user click it the first time it turns green
When the click it a second time it underline the text
when they click it a third time, I need it to go back to gray.

Then 3rd part is not working.

anyone know what I'm missing here?

Ross
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
On the 1st click, the button is gray so it does the first part of the IF. No problem.

The 2nd click, the button is not gray. The 1st part of the IF is skipped and the 2nd part is run because the font is Bold
Code:
ElseIf .Font.Bold = True Then .Font.Underline = True

The font Bold is not changed. So every time after that you will always have ElseIf .Font.Bold = True be TRUE. So you will always run the 2nd part of the IF on subsequent clicks.

On the second click, you should change the .Font.Bold to False or test if the font is underlined.
 
Upvote 0
or something like this - just different approach - but easy to add to as well as modify


Code:
With Button
    Select Case .BackColor
        Case &H8000000F
            your code
        Case &H80FF80
            your code
        Case &H8000000F
            your code        
    End Select
End With
 
Upvote 0
Rasm--I don't think the third part of your select case would get run as the first part is the same.

This should work.

Code:
Sub Toggle(Button)
With Button
    If .BackColor = &H8000000F Then 'if its gray
        .BackColor = &H80FF80 'turn green
        .Font.Bold = True
        .Font.Size = 70
        .Left = .Left - 25
        .Width = .Width + 50
        .Top = .Top - 25
        .Height = .Height + 50
        
    ElseIf .Font.Bold = True And .Font.Underline = False Then 
        .Font.Underline = True
        
    Else 'turn back to gray
        .BackColor = &H8000000F
        .Font.Bold = False
        .Font.Size = 30
        .Left = .Left + 25
        .Width = .Width - 50
        .Top = .Top + 25
        .Height = .Height - 50
        .Font.Underline = False
    End If
End With
End Sub


Private Sub CommandButton1_Click()
    Call Toggle(CommandButton1)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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