skip to Main Content

I’m working in Excel on a Userform. Essentially, I want a “Photoshop-esque” toolbar that floats over my spreadsheet while I work, allowing me to select and use various tools.

I’ve got a series of toggle buttons set up so that when one is clicked, any other toggle buttons go back to unclicked. It’s a little more complicated because I have sub-buttons, if that makes sense, meaning that once I have a button clicked, I can click one of four other buttons to make my actual selection, and these four buttons are mutually exclusive from each other as well.

The weird thing: I haven’t been able to get these buttons to work. Except. For some reason, when I right-click only, the buttons work like a charm. Left-click: nothing. Help please?

Sample button code:

Private Sub tMouse_MouseUp(ByVal button As Integer, _
       ByVal shift As Integer, ByVal X As Single, ByVal Y As Single)


tMouse.Value = True
tActual.Value = False
tSched.Value = False
tX.Value = False
tDiam.Value = False
tCirc.Value = False
tTri.Value = False
tTrash.Value = False
tText.Value = False

End Sub

EDIT:
I tried what was suggested about printing the value of the toggle button. And my computer blew up with messageboxes. I had changed all the actions to Click() events. Apparently I was sending the computer through an infinite loop. Maybe the act of changing a button from true to false or vice versa acts like a click and triggers all the other click events?

3

Answers


  1. I would recommend using an “Option” Control instead of a “Toggle” Control.
    If you stick 4 options in 1 frame then only 1 of those 4 options will allow itself to be true at a time automatically. You should use the “ToggleButton” control only if you want multiple instances of it to be true at the same time.

    However if you refuse to do so and just really want to use ToggleButtons. Then you could write a procedure that is executed once pressing the button that sends it’s name (or something else that identifies it uniquely) as a parameter to a procedure that sets all other togglebuttons false except it.

    Cheers!

    Form Reference

    Private Sub ToggleButton1_Click()
        Dim s As String
        s = "ToggleButton1"
        Evaluate_Options s
    End Sub
    
    Private Sub ToggleButton2_Click()
        Dim s As String
        s = "ToggleButton2"
        Evaluate_Options s
    End Sub
    
    Private Sub ToggleButton3_Click()
        Dim s As String
        s = "ToggleButton3"
        Evaluate_Options s
    End Sub
    
    Private Sub ToggleButton4_Click()
        Dim s As String
        s = "ToggleButton4"
        Evaluate_Options s
    End Sub
    
    Private Sub ToggleButton5_Click()
        Dim s As String
        s = "ToggleButton5"
        Evaluate_Options s
    End Sub
    
    Private Sub ToggleButton6_Click()
        Dim s As String
        s = "ToggleButton6"
        Evaluate_Options s
    End Sub
    
    Private Sub ToggleButton7_Click()
        Dim s As String
        s = "ToggleButton7"
        Evaluate_Options s
    End Sub
    
    Private Sub ToggleButton8_Click()
        Dim s As String
        s = "ToggleButton8"
        Evaluate_Options s
    End Sub
    
    Private Sub Evaluate_Options(s As String)
        Dim tgl As Control
    
        For Each tgl In UserForm1.Frame1.Controls
            If InStr(tgl.Name, s) Then Set_Toggles_1 tgl
        Next
        For Each tgl In UserForm1.Frame2.Controls
            If InStr(tgl.Name, s) Then Set_Toggles_2 tgl
        Next
    End Sub
    
    Private Sub Set_Toggles_1(tglTrue As Control)
        Dim tglFalse As Control
        For Each tglFalse In UserForm1.Frame1.Controls
            If tglFalse.Name = tglTrue.Name Then tglFalse = True Else tglFalse = False
        Next
    End Sub
    
    Private Sub Set_Toggles_2(tglTrue As Control)
    Dim tglFalse As Control
        For Each tglFalse In UserForm1.Frame2.Controls
            If tglFalse.Name = tglTrue.Name Then tglFalse = True Else tglFalse = False
        Next
    End Sub
    
    Login or Signup to reply.
  2. Try these basic mouse event capture subs and modify to suit your needs (tMouse = toggle button name):

    Private Sub tMouse_Click()
    'MsgBox "tb value = " & tMouse.Value
    End Sub
    

    Note: upper sub will not work, if lower sub called

    Private Sub tMouse_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    
    If Button = 1 Then MsgBox "Left"
    If Button = 2 Then MsgBox "Right"
    End Sub
    
    Login or Signup to reply.
  3. You have events that are triggering other events. What you’ll need to do is set a Boolean AllowEventsToRun variable (either at the module or public level) and set that to false at the start of your code. Run whatever you need to do, and then set it to true at the end.

    The trick is to do an if statement to make sure that AllowEventsToRun is set to true before any other code is running. Be sure to initialize the Boolean to true when you load your userform (since the default value of a boolean is false. So something like this:

    Option Explicit
    Private AllowEventsToRun As Boolean
    
    Private Sub ToggleButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If AllowEventsToRun Then
    
        AllowEventsToRun = False
    
        'whatever you're doing that's causing the events to chain fire
    
        AllowEventsToRun = True
    End If
    
    End Sub
    
    
    Private Sub ToggleButton2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If AllowEventsToRun Then
    
        AllowEventsToRun = False
    
        'whatever you're doing that's causing the events to chain fire
    
        AllowEventsToRun = True
    End If
    End Sub
    
    
    Private Sub UserForm_Initialize()
    AllowEventsToRun = True
    End Sub
    

    source: http://www.cpearson.com/excel/SuppressChangeInForms.htm

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search