【问题标题】:Check Captions of Buttons Before Filling Caption of Button without Caption在填充没有标题的按钮标题之前检查按钮的标题
【发布时间】:2018-07-06 04:15:35
【问题描述】:

在 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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    希望我正确理解了您的问题并且您正在使用 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
    

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

    确保将 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
    

    【讨论】:

    • 是的!通过一些调整,这达到了我想要的效果。我为 btCollectionPTE 制作了一个额外的集合
    • @Code123 太好了!我很高兴这对你有所帮助。我现在添加了处理文本框部分的建议。
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2021-06-07
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多