ActiveX Button to Change Font Color of Other Buttons

David20

Board Regular
Joined
May 7, 2009
Messages
57
I'm working on a simple sports stat tracker. I'm using ActiveX buttons to track player fouls. I'd like to be able to see at a glance what fouls were in the first half vs the second half. Here's the VBA code I've patched together so far:

Private Sub CommandButton2_Click()
Dim x As Integer
For x = 1 To 392
If OLEObjects("ToggleButton" & x).Object.Value = True Then
OLEFormat("ToggleButton" & x).Object.Font.Color = vbRed
End If
Next x
End Sub

Basically, by pressing CommandButton2, the macro needs to check each button (yes, there's 392 of them) to see if it has already been pressed (= True) and, if so, change the font color to red. Right now, when I click CommandButton2 I get "Compile Error: Sub or Function not defined." Using Excel 2013 on PC, if that helps.

Thank you in advance!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this:

VBA Code:
Private Sub CommandButton2_Click()
  Dim ctrl As OLEObject
  For Each ctrl In Me.OLEObjects
    If TypeName(ctrl.Object) = "ToggleButton" Then
      If ctrl.Object.Value = True Then
        ctrl.Object.ForeColor = vbRed
      Else
        ctrl.Object.ForeColor = vbBlack
      End If
    End If
  Next
End Sub
 
Upvote 0
Solution
Coming from Access here. Buttons don't have a font color property. You wan forecolor.
I don't see anything in your code to deal with the probability that a toggle that is already true becomes false. Either you need to set a function call for each one of them, or you will have to re-click button #2 afterwards. Selecting 300+ controls and setting their click event to a function is the easy part. The hard part would be if you care to know which one was clicked.

In the sample code, once red it will stay red until you close and reopen.
 
Last edited:
Upvote 0
Corrected code inside the IF statement:
VBA Code:
Private Sub CommandButton2_Click()
    Dim x As Integer
    For x = 1 To 392
        If OLEObjects("ToggleButton" & x).Object.Value = True Then
            OLEObjects("ToggleButton" & x).Object.ForeColor = vbRed
        End If
    Next x
End Sub
 
Upvote 0
Im glad to help you. Thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,166
Members
448,870
Latest member
max_pedreira

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