【问题标题】:Multiple Command Buttons多个命令按钮
【发布时间】:2020-05-17 06:04:18
【问题描述】:

我正在制作一张表格来帮助数独,并有从 1 到 9 的命令按钮,按下时从数字变为空白。 每个按钮的代码是:-

Private Sub CommandButton1_Click()
If CommandButton1.Caption = "1" Then CommandButton1.Caption = "" Else CommandButton1.Caption = "1"
End Sub

Private Sub CommandButton2_Click()
If CommandButton2.Caption = "2" Then CommandButton2.Caption = "" Else CommandButton2.Caption = "2"
End Sub

Private Sub CommandButton3_Click()
If CommandButton3.Caption = "3" Then CommandButton3.Caption = "" Else CommandButton3.Caption = "3"
End Sub

Private Sub CommandButton4_Click()
If CommandButton4.Caption = "4" Then CommandButton4.Caption = "" Else CommandButton4.Caption = "4"
End Sub

Private Sub CommandButton5_Click()
If CommandButton5.Caption = "5" Then CommandButton5.Caption = "" Else CommandButton5.Caption = "5"
End Sub

Private Sub CommandButton6_Click()
If CommandButton6.Caption = "6" Then CommandButton6.Caption = "" Else CommandButton6.Caption = "6"
End Sub

Private Sub CommandButton7_Click()
If CommandButton7.Caption = "7" Then CommandButton7.Caption = "" Else CommandButton7.Caption = "7"
End Sub

Private Sub CommandButton8_Click()
If CommandButton8.Caption = "8" Then CommandButton8.Caption = "" Else CommandButton8.Caption = "8"
End Sub

Private Sub CommandButton9_Click()
If CommandButton9.Caption = "9" Then CommandButton9.Caption = "" Else CommandButton9.Caption = "9"
End Sub

我想复制并过去这九个按钮,并将代码保留在命令按钮中,但在代码中更改按钮编号,但其余代码保持不变。有可能吗。

Private Sub CommandButton10_Click()
If CommandButton1.Caption = "1" Then CommandButton1.Caption = "" Else CommandButton1.Caption = "1"
End Sub

【问题讨论】:

    标签: excel vba button command


    【解决方案1】:

    您可以为此创建一个class resp. control array(另一个示例here),但对您来说最简单的方法可能就是使用以下子

    Sub chCaption(newCaption As String, ctrl As MSForms.Control)
        With ctrl
            If .Caption = newCaption Then
                .Caption = ""
            Else
                .Caption = newCaption
            End If
        End With
    End Sub
    

    这样

    Private Sub CommandButton1_Click()
        chCaption "1", Me.CommandButton1
    End Sub
    
    Private Sub CommandButton2_Click()
        chCaption "2", Me.CommandButton2
    End Sub
    

    【讨论】:

      【解决方案2】:

      我相信下面的代码会做你想做的事。将其安装在具有命令按钮的代码表中,或将其公开并安装在标准代码模块中,具体取决于您是只需要从一张表中调用它还是从几张表中调用它。

      Private Sub ResetButton(ByVal Id As Integer)
          ' 028
      
          Dim BtnVal As Integer
      
          BtnVal = Id Mod 9
          If BtnVal = 0 Then BtnVal = 9
      
          With ActiveSheet.OLEObjects("CommandButton" & Id).Object
              .Caption = IIf(Len(.Caption), "", BtnVal)
          End With
      End Sub
      

      此代码使用 MOD 函数从 CommandButton 编号中提取 Soduko 编号。 CommandButtons1 到 9 将包含数字 1 到 9 以及 10 到 18、19 到 27 等。对于每个按钮,您需要在工作表的代码模块中显示如下所示的过程。

      Private Sub CommandButton1_Click()
          ' 028
          ResetButton 1
      End Sub
      

      每个副本中的参数都不同,与它响应的命令按钮的编号相同。

      虽然这应该可以满足您的要求,但我怀疑这是完成任务的最佳方式。我推荐一个形状而不是命令按钮,比如一个更容易编程和绘制的矩形。假设您有 9 个名为 Rectangle 1 到 9 的矩形,您将为所有矩形分配相同的代码,无需任何自定义。这段代码。

      Sub ResetSodukoButton()
          ' 028
      
          Dim Caller As String
          Dim BtnVal  As Integer
      
          Caller = Application.Caller
          BtnVal = Split(Caller)(1) Mod 9
          If BtnVal = 0 Then BtnVal = 9
      
          With ActiveSheet.Shapes(Caller).TextFrame.Characters
              .Text = IIf(Len(.Text), "", BtnVal)
          End With
      End Sub
      

      代码将识别被单击的形状,读取其名称,使用 MOD 函数的相同解释从中生成数独数字,并相应地设置数字。您可以省去所有 CommandButtonX_Click 程序的麻烦。

      在标准代码模块中安装子ResetSodukoButton

      【讨论】:

        猜你喜欢
        • 2021-07-08
        • 2012-12-01
        • 1970-01-01
        • 2017-06-19
        • 1970-01-01
        • 2013-04-03
        • 2015-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多