单击后,将ActiveX命令button颜色更改回到以前的颜色

我有一个超过65个ActiveX命令button的电子表格。 当我离开时单击一个命令button,它变成绿色,并在单元格中添加(+1)。 当我右键单击相同的命令button,它变成红色,并在单元格中添加(+1)。

当我点击另一个命令button时,我想将上一个命令button返回到默认的灰色。 问题是前面的命令button保持与我之前点击过的相同的颜色。

如何使单击的命令button返回到默认灰色,当表单上有65个以上的命令button时。 这是我到目前为止的一个单一的命令button:

Private Sub Action68_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If Button = 1 Then Worksheets("Stats").Cells(CurrentPlayerRow, "BA").Value = Worksheets("Stats").Cells(CurrentPlayerRow, "BA").Value + 1 Action68.BackColor = vbGreen ElseIf Button = 2 Then Worksheets("Stats").Cells(CurrentPlayerRow, "BB").Value = Worksheets("Stats").Cells(CurrentPlayerRow, "BB").Value + 1 Action68.BackColor = vbRed End If End Sub Private Sub Action69_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If Button = 1 Then Worksheets("Stats").Cells(CurrentPlayerRow, "BT").Value = Worksheets("Stats").Cells(CurrentPlayerRow, "BT").Value + 1 Action69.BackColor = vbGreen ElseIf Button = 2 Then Worksheets("Stats").Cells(CurrentPlayerRow, "BU").Value = Worksheets("Stats").Cells(CurrentPlayerRow, "BU").Value + 1 Action69.BackColor = vbRed End If End Sub 

当它是正确的或者左键单击的时候,它会将颜色更改为红色或绿色。 但是当我点击另一个button时,我不知道如何将它变成默认的灰色。

基本上,当我点击“动作69”命令button时,“动作68”命令button和其他67个命令button返回到默认的灰色,使得颜色只改变被点击的button。 你有什么build议吗?

谢谢

这是很多复制粘贴和重复的代码。 你会想减less这个重复,这样当你需要button来做别的事情(或者只是改变颜色scheme)的时候,你有一个地方可以改变,而不是70。

您可以通过增加抽象层次来实现,即通过在单独的专用过程中实现function。

 Public Enum ButtonState LeftButton = 1 RightButton = 2 End Enum Private Sub HandleControlClick(ByVal axControl As MSForms.Control, ByVal column As String, ByVal state As ButtonState) Const defaultColor As Long = &H8000000F& Dim newColor As Long, columnOffset As Long Select Case state Case LeftButton newColor = vbRed Case RightButton newColor = vbGreen columnOffset = 1 Case Else newColor = defaultColor End Select axControl.BackColor = newColor StatsSheet.Cells(CurrentPlayerRow, column).Offset(0, columnOffset).Value = StatsSheet.Cells(CurrentPlayerRow, column).Offset(0, columnOffset).Value + 1 End Sub 

现在你的处理者可以看起来像这样:

 Private Sub Action68_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) HandleControlClick ActiveSheet.OleObjects("Action68").Object, Button, "BA" End Sub Private Sub Action69_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) HandleControlClick ActiveSheet.OleObjects("Action69").Object, Button, "BT" End Sub 

如果可能的话,我会热烈地build议你给你的Worksheets("Stats")一个(Name) statsSheet (或类似的) – 这样你使用一个已经存在的工作表对象,而不是每次从Worksheets集合中获取它。

这里是一些演示代码,只对工作表上的所有button使用一个事件处理程序

把它放到名为BtnClass class module

这是工作表上所有button的事件处理程序

 ' -------------------------------------------------------------------------------------- Option Explicit Public WithEvents ButtonGroup As MSForms.CommandButton Private Sub ButtonGroup_Click() Dim msg As String msg = "clicked : " & ButtonGroup.Name & vbCrLf _ & "caption : " & ButtonGroup.Caption & vbCrLf _ & "top : " & ButtonGroup.Top & vbCrLf _ & "left : " & ButtonGroup.Left Debug.Print ButtonGroup.Name; vbNewLine; msg End Sub Private Sub ButtonGroup_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single) Debug.Print "down", Button, ButtonGroup.Name If Button = 1 Then ButtonGroup.BackColor = vbRed ButtonGroup.TopLeftCell.Offset(0, 3).Interior.Color = vbBlue Else ButtonGroup.BackColor = vbGreen ButtonGroup.TopLeftCell.Offset(0, 3).Interior.Color = vbYellow End If End Sub Private Sub ButtonGroup_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single) Debug.Print "up", ButtonGroup.Name ButtonGroup.BackColor = &H8000000F End Sub ' -------------------------------------------------------------------------------------- 

把它放到sheet模块中

 ' -------------------------------------------------------------------------------------- Private Sub Worksheet_Activate() activateButtons End Sub ' -------------------------------------------------------------------------------------- 

把这个模块

makeButtons在工作表上创build一堆button

activateButtons将button附加到类事件处理程序

 ' -------------------------------------------------------------------------------------- Option Explicit Dim Buttons() As New BtnClass Const numButtons = 20 ' Sub doButtons() makeButtons ' does not work reliably ... buttons out of sequence activateButtons ' does not activate reliably (run these separately instead) End Sub Sub makeButtons() ' creates a column of commandButtons Dim sht As Worksheet Set sht = ActiveSheet Dim i As Integer For i = 1 To sht.Shapes.Count ' Debug.Print sht.Shapes(1).Properties sht.Shapes(1).Delete DoEvents Next i Dim xSize As Integer: xSize = 2 ' horizontal size (number of cells) Dim ySize As Integer: ySize = 2 ' vertical size Dim t As Range Set t = sht.Range("d2").Resize(ySize, xSize) For i = 1 To numButtons sht.Shapes.AddOLEObject Left:=t.Left, Top:=t.Top, Width:=t.Width, Height:=t.Height, ClassType:="Forms.CommandButton.1" DoEvents Set t = t.Offset(ySize) Next i End Sub Sub activateButtons() ' assigns all buttons on worksheet to BtnClass.ButtonGroup Dim sht As Worksheet Set sht = ActiveSheet ReDim Buttons(1 To 1) Dim i As Integer For i = 1 To sht.Shapes.Count ReDim Preserve Buttons(1 To i) Set Buttons(i).ButtonGroup = sht.Shapes(i).OLEFormat.Object.Object Next i End Sub ' --------------------------------------------------------------------------------------