【问题标题】:How to assign code to a dynamically created button?如何将代码分配给动态创建的按钮?
【发布时间】:2015-07-14 19:13:27
【问题描述】:

首先:我是 VBA 的初学者,我对 UserForms 的工作原理一无所知。

也就是说我正在尝试将代码分配给 3 个动态创建的命令按钮。

经过一些研究,遇到this页面,我只是写了一个代码来编写按钮的代码。问题是,我需要分发此工作簿,因此这种方法不再适用。

我研究了很多(1234)并遇到了this 的帖子。我试图做@SiddharthRout 做的例子,但我没有成功。我试图了解 ClassModule 是如何工作的,但我不能(12)。我认为与@SiddharthRout 完全相同的代码可以解决我的问题,但我无法使其在普通模块上运行。

长话短说:我需要一个代码来将代码分配给命令按钮而不使用可扩展性(编写代码的代码)。

编辑
我想在普通工作表上创建这些按钮,而不是在用户窗体上。

【问题讨论】:

    标签: vba class excel excel-2010


    【解决方案1】:

    阅读: http://scriptorium.serve-it.nl/view.php?sid=13

    Sub MakeForm()
        Dim TempForm As Object ' VBComponent
        Dim FormName As String
        Dim NewButton As MSForms.CommandButton
        Dim TextLocation As Integer
        ' ** Additional variable
        Dim X As Integer
    
        'Locks Excel spreadsheet and speeds up form processing
        Application.VBE.MainWindow.Visible = False
        Application.ScreenUpdating = False
        ' Create the UserForm
        Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
        'Set Properties for TempForm
        With TempForm
        .Properties("Caption") = "Temporary Form"
        .Properties("Width") = 200
        .Properties("Height") = 100
        End With
        FormName = TempForm.Name
    
        ' Add a CommandButton
        Set NewButton = TempForm.Designer.Controls _
        .Add("forms.CommandButton.1")
        With NewButton
        .Caption = "Click Me"
        .Left = 60
        .Top = 40
        End With
        ' Add an event-hander sub for the CommandButton
        With TempForm.CodeModule
    
        ' ** Add/change next 5 lines
        ' This code adds the commands/event handlers to the form
        X = .CountOfLines
        .InsertLines X + 1, "Sub CommandButton1_Click()"
        .InsertLines X + 2, "MsgBox ""Hello!"""
        .InsertLines X + 3, "Unload Me"
        .InsertLines X + 4, "End Sub"
        End With
    
        ' Show the form
        VBA.UserForms.Add(FormName).Show
        '
        ' Delete the form
        ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm
      End Sub 
    

    【讨论】:

    • 请详细说明。 OP 是初学者,需要更多帮助。
    • 谢谢你的回答,但是我不够清楚。我想在普通工作表上创建这些按钮,而不是在用户窗体上。
    【解决方案2】:

    This code from here:

    Sub CreateButtons()
      Dim arrNames            As Variant
      Dim arrCaptions         As Variant
      Dim Wkb                 As Workbook
      Dim Wks                 As Worksheet
      Dim NewBtn              As OLEObject
      Dim Code                As String
      Dim NextLine            As Long
      Dim LeftPos             As Long
      Dim Gap                 As Long
      Dim i                   As Long
    
      'The Workbook, where...
      Set Wkb = ActiveWorkbook
      '... the worksheet is, where the button will be created
      ' and code will be written.
      Set Wks = Wkb.Worksheets(1)
    
      'Commandbuttons' CODENAMES in array
      arrNames = Array("cmbName1", "cmbName2", "cmbName3")
      'Commandbuttons' captions in array
      arrCaptions = Array("First Task", "Second Task", "Third Task")
    
      'Button pos.
      LeftPos = 100
    
      Gap = 15
    
      For i = LBound(arrNames) To UBound(arrNames)
    
        'Add a CommandButton to worksheet
        Set NewBtn = Wks.OLEObjects.Add(ClassType:="Forms.CommandButton.1")
    
        'Set button's properties
        With NewBtn
            .Left = LeftPos
            .Top = 5
            .Width = 65
            .Height = 30
            .Name = arrNames(i)
            .Object.Caption = arrCaptions(i)
            .Object.Font.Size = 10
            .Object.Font.Bold = True
            .Object.Font.Name = "Times New Roman"
        End With
    
        'Add the event handler code
        Code = "Sub " & NewBtn.Name & "_Click()" & vbCrLf
        Code = Code & "     MsgBox ""Hello...""" & vbCrLf
        Code = Code & "End Sub"
    
        '"Code" is a string
        With Wkb.VBProject.VBComponents(Wks.CodeName).CodeModule
            'Find last line in Codemodule
            NextLine = .CountOfLines + 1
            .InsertLines NextLine, Code
        End With
    
        'NEXT button's pos.
        LeftPos = LeftPos + NewBtn.Width + Gap
    
      Next i
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多