简体   繁体   中英

Change Color of ActiveX Command Button with Macro

As a part of a spreadsheet I'm making, I want a macro to change the color of an ActiveX button. Right now I have the button change colors when it is clicked. I want it to change back to the original color when a separate macro is run.

Any thoughts are appreciated, thank you!

Let's assume that the Button is white:
Macro1:

Sheet(n).CommandButton1.BackColor = 'something

Macro2:

Sheet(n).CommandButton1.BackColor = &H00FFFFFF&

Shouldn't this be the only thing you have to do? (More)

You are probably looking for something like this:

Option Explicit

Sub tmpSO()

Dim Sh As Worksheet
Dim Obj As OLEObject

For Each Sh In ThisWorkbook.Worksheets
    For Each Obj In Sh.OLEObjects
        If TypeName(Obj.Object) = "CommandButton" Then
            Debug.Print Obj.Name
            If Obj.Object.BackColor = -2147483633 Then
                Obj.Object.BackColor = 6740479
            Else
                Obj.Object.BackColor = -2147483633
            End If
        End If
    Next Obj
Next Sh

End Sub

Note, that the .Name resides directly on the .OLEObject while the .BackColor is a sub-element of .OLEObject.Object .

The above code will toggle all buttons background color between the greyish (default) color and an orange color.

Let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM