向VBA的dyn按钮添加Click方法不起作用

问题描述 投票:0回答:1

我想创建动态按钮,然后实现click方法,但是Internet上的任何帮助在这里都行不通。我希望有人可以在这里帮助我。

无法创建所有按钮。不幸的是,由于我们只能使用Excel并且可能使用其他语言,因此无法编写其他语言。

这是关于infoBtn1_Click

我的脚本

Option Explicit

Dim WithEvents infoBtn          As MSForms.CommandButton
Dim WithEvents infoBtn1          As MSForms.CommandButton
Dim WithEvents frameCard        As MSForms.frame
Dim WithEvents cardTitel        As MSForms.Label
Dim WithEvents ausLabel         As MSForms.Label
Dim WithEvents ausbilderLabel   As MSForms.Label
Dim WithEvents amLabel          As MSForms.Label
Dim WithEvents datumLabel       As MSForms.Label
Dim WithEvents infoLabel        As MSForms.Label

Dim add                         As Integer
Dim topPos                      As Integer
Dim ctl                         As Control
Dim n                           As Integer
Dim VorhabenArray()             As Variant
Dim Free(1 To 5)                As Long
Dim sh                          As Worksheet
Dim v                           As Range
Dim arr(0 To 40)                As Integer
Dim i                           As Integer
Dim ausbildungNr                As String
Dim speicher                    As String


Private Sub CommandButton1_Click()
    If ComboBox1.Value = "" Then
    MsgBox "Bitte tragen Sie eine Ausbildung ein."
    Exit Sub
    End If
    ausbildungSuche.ausbildungCB.Value = ComboBox1.Value
    Unload Me
    Call ausbildungSuche.suchenBtn_Click
End Sub

Private Sub infoBtn1_Click()
   MsgBox "test"
End Sub


Private Sub UserForm_Activate()


    On Error GoTo fehler

    ComboBox1.List = Sheets("Meta").Range("A1:A8").Value
    speicher = ausbildungSuche.ausbildungCB.Value

Select Case speicher
    Case "MilFit":
        ausbildungNr = "1"
    Case "Circuit training":
        ausbildungNr = "2"
    Case "Volleyball":
        ausbildungNr = "3"
    Case "Fußball":
        ausbildungNr = "4"
    Case "Sportliche Ertuechtigung":
        ausbildungNr = "5"
    Case "BFT":
        ausbildungNr = "6"
    Case "DSA":
        ausbildungNr = "7"
    Case "Schwimmen":
        ausbildungNr = "8"
    Case Else
        MsgBox "Fehler"
End Select


    Set sh = ThisWorkbook.Worksheets(ausbildungNr)

    i = 0
    topPos = 12

    For Each v In sh.Range("M2:M100")
        If Not v = "0" Then

            Set frameCard = Controls.add("Forms.Frame.1", "frame" & i)
            With frameCard
                .Left = 144
                .Top = topPos
                .Width = 258
                .Height = 72
                .Caption = ""
                .Zoom = 100
                .SpecialEffect = 3
                .BorderColor = &H80000012
            End With

            Set cardTitel = frameCard.Controls.add("Forms.Label.1", "cardTitel" & i, True)
             With cardTitel
                .Left = 8
                .Top = 6
                .Width = 126
                .Height = 18
                .ForeColor = &H8000000D
                .Caption = v.Cells(, -10)
                .FontBold = True
                .FontSize = 12
            End With

            Set infoBtn = frameCard.Controls.add("Forms.CommandButton.1", "infoBtn" & i, True)
            With infoBtn
                .Left = 144
                .Top = 36
                .Width = 102
                .Height = 24
                .ForeColor = &HFFFFFF
                .BackColor = &H8000000D
                .Caption = v & " Plätze frei"
            End With

            Debug.Print "infoBtn" & i

            Set ausLabel = frameCard.Controls.add("Forms.Label.1", "ausLabel", Visible)
            With ausLabel
                .Left = 12
                .Top = 30
                .Width = 42
                .Height = 12
                .Caption = "Ausbilder"
            End With

            Set ausbilderLabel = frameCard.Controls.add("Forms.Label.1", "ausbilderLabel", Visible)
            With ausbilderLabel
                .Left = 54
                .Top = 30
                .Width = 72
                .Height = 12
                .FontBold = True
                .Caption = v.Cells(, -9)
            End With

            Set amLabel = frameCard.Controls.add("Forms.Label.1", "amLabel", Visible)
            With amLabel
                .Left = 12
                .Top = 48
                .Width = 24
                .Height = 12
                .Caption = "Am"
            End With

            Set datumLabel = frameCard.Controls.add("Forms.Label.1", "datumLabel", Visible)
            With datumLabel
                .Left = 54
                .Top = 48
                .Width = 72
                .Height = 12
                .FontBold = True
                .Caption = v.Cells(, -8)
            End With

            Set infoLabel = frameCard.Controls.add("Forms.Label.1", "infoLabel", Visible)
            With infoLabel
                .Left = 222
                .Top = 6
                .Width = 24
                .Height = 12
                .FontBold = True
                .Caption = "Info"
            End With

            topPos = frameCard.Top + frameCard.Height + 10
            i = i + 1
        End If
    Next

   ausbildungsfilter.Caption = ausbildungSuche.ausbildungCB.Value
    Exit Sub

fehler:     MsgBox "Das hat leider nicht geklappt."
    Unload Me


End Sub
excel vba vb.net
1个回答
0
投票

您必须将其分配给按钮的OnClick属性(通常是在OnLoad事件或类似事件上:)

infoBtn1.OnClick = "[Event Procedure]"

这假定您已像在示例代码中一样声明了Private Sub InfoBtn1_Click过程。并请注意,应该将此确切的"[Event Procedure]"字符串分配给OnClick

© www.soinside.com 2019 - 2024. All rights reserved.