【问题标题】:Simplify toggle button change BackColor Code VBA简化切换按钮更改背景颜色代码 VBA
【发布时间】:2016-12-05 16:40:18
【问题描述】:

我是 VBA 制作的新手,所以下面的所有代码仍然可以工作,但它需要很多代码行。即使它更容易维护,但如果有人可以简化我的菜鸟代码以减少一些行并且更令人赏心悦目?

我的用户表单中有 20 多个切换按钮

这是我的代码示例,需要帮助以使其更简单

Private Sub tgglC_Result1_Click()
If tgglC_Result1.Value = True Then
    tgglC_Result1.BackColor = &HFF00&
    tgglNC_Result1.Enabled = False
    lblResult1.Caption = Now
    lblResult1.Visible = True
Else
    tgglC_Result1.BackColor = &H8000000F
    tgglNC_Result1.Enabled = True
    lblResult1.Visible = False
End If
End Sub
Private Sub tgglC_Result2_Click()
If tgglC_Result2.Value = True Then
    tgglC_Result2.BackColor = &HFF00&
    tgglNC_Result2.Enabled = False
    lblResult2.Caption = Now
    lblResult2.Visible = True
Else
    tgglC_Result2.BackColor = &H8000000F
    tgglNC_Result2.Enabled = True
    lblResult2.Visible = False
End If
End Sub
Private Sub tgglC_Result3_Click()
If tgglC_Result3.Value = True Then
    tgglC_Result3.BackColor = &HFF00&
    tgglNC_Result3.Enabled = False
    lblResult3.Caption = Now
    lblResult3.Visible = True
Else
    tgglC_Result3.BackColor = &H8000000F
    tgglNC_Result3.Enabled = True
    lblResult3.Visible = False
End If
End Sub
Private Sub tgglC_Result4_Click()
If tgglC_Result4.Value = True Then
    tgglC_Result4.BackColor = &HFF00&
    tgglNC_Result4.Enabled = False
    lblResult4.Caption = Now
    lblResult4.Visible = True
Else
    tgglC_Result4.BackColor = &H8000000F
    tgglNC_Result4.Enabled = True
    lblResult4.Visible = False
End If
End Sub

【问题讨论】:

标签: vba excel


【解决方案1】:

最好的方法应该是使用一个类

但更“传统”的方式也可以帮助您减轻打字负担:

  • 定义一个独特的切换控件处理子

        Private Sub tgglC_Result_Click()
            Dim NC As Control
    
            With Me
                Set NC = .Controls(VBA.Replace(.ActiveControl.Name, "tgglC", "tgglNC")) '<--| set the "counter part" toggle button control of the "Active" control (i.e. the one being currently toggled)
                With .ActiveControl
                    .BackColor = IIf(.Value, &HFF00&, &H8000000F)
                    NC.Enabled = Not .Value
                End With
            End With
        End Sub
    
  • 从您的任何事件处理程序中调用它

    Private Sub tgglC_Result1_Click()
        tgglC_Result_Click
    End Sub
    
    Private Sub tgglC_Result2_Click()
        tgglC_Result_Click
    End Sub
    
    Private Sub tgglC_Result3_Click()
        tgglC_Result_Click
    End Sub
    
    ...
    

【讨论】:

  • 它对我不起作用,第一次单击 (value = true) 按钮变为绿色,但当第二次单击 (value = false) 时,它仍然是绿色的。我不明白如何编辑代码
  • 我已经对其进行了测试,正如您所写,它可以与用户表单中的那些切换按钮一起使用。您是否在其他控件(如 Frame)中有这些按钮?
  • 哦,是的,我忘了,它在 MultiPage 里面。我应该改变什么?
  • 如果所有这些控件都在 same 容器控件中,例如 MultiPage,则相同的代码可以工作
  • 都在multipage.pages(1)中,如何编辑代码?
【解决方案2】:

这并不是一个真正的简化解决方案,但当我需要为 Access 子表单上的 60 多个控件提供逻辑时,我使用了这个解决方案(与您的任务类似):

Sub makeCode()
Dim i As Integer    
For i = 1 To 4
        Debug.Print "Private Sub tgglC_Result" & i & "_Click()"
        Debug.Print "tgglC_Result" & i & ".BackColor = &HFF00&"
        Debug.Print "tgglNC_Result2.Enabled = False"
        Debug.Print "lblResult" & i & ".Caption = Now"
        Debug.Print "lblResult" & i & ".Visible = True"
        Debug.Print "End Sub"
        Debug.Print ""
    Next
End Sub

将立即窗口中的结果复制到代码编辑器中。更改所有子程序也很容易:只需更改循环体,运行它,然后替换旧代码。

【讨论】:

  • 我不明白如何使用你的代码,我应该在你的代码上创建一个私有子吗?当我尝试它没有工作
  • 是的,我修改了答案以澄清这一点。
  • 私人子 tgglC_Result1_Click() makecode end sub ,不起作用。完全没有颜色变化。我错了吗?
  • 是的,我的回答有一个错误 - 它必须是一个子,而不是一个私人子。立即复制并运行。
  • 仍然,颜色没有改变。我不知道发生了什么
猜你喜欢
  • 2017-08-19
  • 2011-03-05
  • 2021-02-10
  • 2013-06-07
  • 2012-09-05
  • 2021-04-27
  • 2011-12-01
  • 2015-06-04
  • 1970-01-01
相关资源
最近更新 更多