在没有标题的情况下填充按钮标题之前检查按钮的标题

时间:2018-01-26 18:59:16

标签: excel-vba vba excel

在Excel VBA中,我尝试在填写其他按钮标题之前检查工作表中所有按钮的标题。

我正在尝试单击名为btn_PTE_Li的命令按钮,并且第一个带有空标题的按钮(在表单中的15个命令按钮中)将填充“Li”(单击的标题)按钮)。此外,在填写该按钮的标题(例如:btn_Element_i)时,文本框tbx_Number_i(i对应于与btn_Element_i相同的整数i)的文本将填充文本“1”。

此外,如果任何按钮已经有标题“Li”,那么另一个没有其他按钮应该填充额外的标题“Li”。

我希望这段代码尽可能通用,这样我就可以将它用于多个按钮而不仅仅是命令按钮btn_PTE_Li。让代码引用点击按钮的标题以填充btn_Element_i的标题会很不错。对于按钮btn_PTE_He,包含“Li”的代码不需要更改为“He”。

以下是我目前用于完成3个按钮btn_Element_1btn_Element_2btn_Element_3的此任务的暂定代码:

Private Sub btn_PTE_Li_Click()
Me.btn_PTE_Li.BackColor = &H80000010
    If Me.btn_Element_1.Caption = "" Then
        Me.btn_Element_1.Caption = "Li"
        Me.tbx_Number_1.Text = "1"
    ElseIf Me.btn_Element_2.Caption = "" And Me.btn_Element_1.Caption <> "Li" Then
        Me.btn_Element_2.Caption = "Li"
        Me.tbx_Number_2.Text = "1"
    ElseIf Me.btn_Element_3.Caption = "" And Me.btn_Element_1.Caption <> "Li" And Me.btn_Element_2.Caption <> "Li" Then
        Me.btn_Element_3.Caption = "Li"
        Me.tbx_Number_3.Text = "1"
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

希望我正确理解您的问题,并且您正在使用 ActiveX控件,如果您希望它是通用的,我建议如下:

创建一个类模块,将其命名为 MyButtonClass 并粘贴以下代码:

Option Explicit

Dim WithEvents btControl As MSForms.CommandButton

Private controlName As String

Public Sub btControl_Click()

    Dim MySheet As Worksheet
    Set MySheet = Sheets("Sheet1")

    If Left(btControl.Name, 7) = "btn_PTE" Then
        Dim btl As Variant
        btControl.BackColor = &H80000010
        'Check if another button has the same caption
        For Each btl In btCollectionElement
            If TypeName(btl) = "CommandButton" And btl.Caption = btControl.Caption Then Exit Sub
        Next btl

        'Fill caption of the first button with empty caption
        For Each btl In btCollectionElement
            If TypeName(btl) = "CommandButton" And btl.Caption = "" Then
                btl.Caption = btControl.Caption
                Exit For
            End If
        Next btl
    End If
End Sub

Public Sub Attach(newBT As MSForms.CommandButton, newName As String)
    Set btControl = newBT
    controlName = newName
End Sub

Private Sub Class_Initialize()
    controlName = ""
End Sub

然后将以下内容粘贴到常规代码模块

Option Explicit

Public groupClickCount As Integer
Public btCollection As Collection
Public btCollectionElement As Collection


Public Sub SetUpControlsOnce()
    Dim MySheet As Worksheet
    Set MySheet = Sheets("Sheet1")

    Dim thisBT As MyButtonClass
    Dim btl As OLEObject
    Dim btControl As MSForms.CommandButton

    If btCollection Is Nothing Then
        Set btCollection = New Collection
    End If

    If btCollectionElement Is Nothing Then
        Set btCollectionElement = New Collection
    End If

    For Each btl In MySheet.OLEObjects
        If TypeName(btl.Object) = "CommandButton" And Left(btl.Name, 11) = "btn_Element" Then
            '--- this is an ActiveX CheckBox
            Set thisBT = New MyButtonClass
            thisBT.Attach btl.Object, btl.Name
            btCollection.Add thisBT
            btCollectionElement.Add btl.Object
        End If
    Next btl

End Sub

Sub Clear_all_Element_Buttons_Captions()

    Dim btl As Variant
    For Each btl In btCollectionElement
        If TypeName(btl) = "CommandButton" Then
            btl.Caption = ""
        End If
    Next btl

End Sub

您还可以将以下内容添加到 ThisWorkbook 模块中,以确保安装宏在打开时运行:

Private Sub Workbook_Open()
    Call SetUpControlsOnce
End Sub

请注意,代码使用按钮的名称来区分按钮的类型(编辑其他按钮的标题和不编辑按钮的标题),但可能还有其他方法可以做到这一点。如果您发现所有按钮的名称都需要很长时间,您可以随时编写一个创建按钮的宏并为您重命名(我建议您查看记录宏工具得到一些见解)。

确保将Sheet1更改为工作表的适当名称(模块和类模块中)。

对于问题的文本框部分,您可以在btControl_Click宏中找到类似的内容:

Dim Counter as integer
'Fill caption of the first button with empty caption
    For Each btl In btCollectionElement
        Counter = Counter + 1
        If TypeName(btl) = "CommandButton" And btl.Caption = "" Then
            btl.Caption = btControl.Caption
            MySheet.Item("Textbox " & Counter).TextFrame2.TextRange = "1"
            Exit For
        End If
    Next btl