VBA遍历UserForm上的按钮 [英] VBA loop through buttons on UserForm

查看:121
本文介绍了VBA遍历UserForm上的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有使用VBA这么新-但是所有搜索都没有给我答案

I have not used VBA so quite new - but all searches have not given me the answer

确实是一个简单的问题.我在Excel表单中有一组按钮.当每个按钮被按下时,代码非常相似,对于每个被按下的按钮,我希望按钮的颜色能够改变.所以实际上,每个按钮都有类似的内容

its a simple question really. I have a group of buttons in an Excel Form. The code is very similar when each one is pressed, and for each pressed button, I would like the colour of the button to change. So in reality, I have something like this for each button

UserForm2.CommandButton17.BackColor = RGB(255,255,0)

我想浏览每个按钮.检查是否按了它,然后相应地设置颜色.

I would like to go through each button. Check if it is pressed, and then set the colour accordingly.

我实际上想说

for counter in 1 to 100
if (ispressed((CommandButton & counter )) then

我发现了以下结构:

Dim ctrl as Control
For Each ctrl in UserForm1.Controls
  ctrl.BackColor = RGB(255,0,0)
end for

此构造有效-但我无法弄清楚如何检查按钮是否被按下.

this construct works - but I cant figure out how to check if the button is pressed.

一些答案​​显示了上述结构,其中 ctrl.Value = True 但是这些是用于复选框和单选按钮的.我什至没有带按钮的 ctrl.Value 选项,所以无论如何我都无法使用它

Some of the answers show the above construct, with ctrl.Value = True but those are for checkboxes and radio buttons. I don't even get the ctrl.Value option with buttons, so I can't use it anyway

我发现每个代码示例都掩盖了这一要求.

Every example of code I have found glosses over this requirement.

有人可以帮忙吗

推荐答案

如果希望它表示状态,请使用ToggleButton代替CommandButton.

Use a ToggleButton instead of a CommandButton if you want it to represent a state.

要为每个切换按钮初始化状态,您可以循环浏览控件.

To initialize a state for each toggle button you can loop through the control.

Dim ctrl As Control
For Each ctrl In Me.Controls
    If TypeName(ctrl) = "ToggleButton" Then
        ctrl.Value = True 'set button state to pressed
    End If
Next ctrl

这会将状态设置为按下表单上的每个切换按钮.

This sets the state as pressed for every toggle button on the form.

请注意, .Value 不会显示在IntelliSense框中,因为 ctrl 属于 Control 类型,没有<代码>.值.如果您需要IntelliSense,则可以采用以下解决方法:

Note that the .Value does not show up in the IntelliSense box because ctrl is of type Control which doesn't have a .Value. If you need IntelliSense then you could workaround like that:

Dim ctrl As Control
For Each ctrl In Me.Controls
    If TypeName(ctrl) = "ToggleButton" Then
        Dim tggl As ToggleButton
        Set tggl = ctrl
        
        tggl.Value = True
    End If
Next ctrl

//编辑

每次单击切换按钮时,都会触发该按钮的 _Click 事件.因此,每个按钮都需要一个这样的事件.

Everytime a toggle button gets clicked it triggers a _Click event for that button. So you will need such an event for each button.

Private Sub ToggleButton1_Click()
    With Me.ToggleButton1
        If .Value = True Then
            .BackColor = RGB(255, 0, 0)
        Else
            .BackColor = -2147483633 'switch to original color
        End If
    End With
End Sub

或者,如果您有许多按钮,则可以更高效地完成操作

Or if you have many buttons, do it more efficiently

'this procedure handles all buttons
Private Sub ToggleButtonClick(ByRef tggl As ToggleButton)
    With tggl
        If .Value = True Then
            .BackColor = RGB(255, 0, 0)
        Else
            .BackColor = -2147483633 'switch to original color
        End If
    End With
End Sub

'so you just need to call that function on every _Click event
Private Sub ToggleButton1_Click()
    ToggleButtonClick Me.ToggleButton1
End Sub

Private Sub ToggleButton2_Click()
    ToggleButtonClick Me.ToggleButton2
End Sub

但是您仍然需要每个按钮的 _Click()事件来调用该过程.

But you still need a _Click() event for every button to call that procedure.

您还可以评估 _Click()事件中每个按钮的 .Value 状态,以设置/取消设置星号.

You can also evaluate the .Value state of each button in the _Click() event to set/unset your asterisk.

这篇关于VBA遍历UserForm上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆