ActiveX Button to Change Font Color of Other Buttons

David20

New Member
Joined
May 7, 2009
Messages
49
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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
15,062
Office Version
  1. 2010
Platform
  1. Windows
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
 
Solution

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
3,455
Office Version
  1. 365
Platform
  1. Windows
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:

shknbk2

Well-known Member
Joined
Mar 5, 2016
Messages
554
Office Version
  1. 365
Platform
  1. Windows
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
15,062
Office Version
  1. 2010
Platform
  1. Windows
Im glad to help you. Thanks for the feedback
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,299
Messages
5,853,140
Members
431,549
Latest member
NnAa

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
Top