Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign code to a button created dynamically

Tags:

I'm trying to get a button I've created dynamically on an excel userform form to run a macro called transfer which I've written in Module 1 of the "Modules" section of my project.

Below I've pasted the code I've written so far in the userform which actually manages to create the Transfer to Sheet button in the frame (which I've also created dynamically) but for some reason, when I run VBA I get a 438 error message saying that Object doesn't support this property or method.

Can anybody tell me how I can resolve this?

Here's the code:

Dim framecontrol1 As Control  Set workitemframe = Controls.Add("Forms.Frame.1") With workitemframe     .Width = 400     .Height = 400     .Top = 160     .Left = 2     .ZOrder (1)     .Visible = True End With  workitemframe.Caption = "Test" Set framecontrol1 = workitemframe.Controls.Add("Forms.commandbutton.1")  With framecontrol1     .Width = 100     .Top = 70     .Left = 10     .ZOrder (1)     .Visible = True     .Caption = "Transfer to Sheet" End With framecontrol1.OnAction = "transfer" 
like image 782
Tony Catton Avatar asked Apr 19 '12 08:04

Tony Catton


People also ask

How do you assign a macro to a button you created?

Click File > Options > Quick Access Toolbar. In the Choose commands from list, click Macros. Select the macro you want to assign a button to. Click Add to move the macro to the list of buttons on the Quick Access Toolbar.

How do I add a button programmatically in VBA?

Select the Button Form Control from the menu. Right click and hold the mouse then drag and release to create your button. The Assign Macro window will pop up and you can select the VBA procedure you want to run from the button. Press the OK button.


2 Answers

Here is an example. Please amend it to suit your needs :)

This example will create a command button and assign code to it so that when it is pressed, it will display "Hello World".

Paste this code in the click event of a command button which will create a new command button dynamically and assign code to it.

Option Explicit  Dim cmdArray() As New Class1  Private Sub CommandButton1_Click()     Dim ctl_Command As Control     Dim i As Long      i = 1      Set ctl_Command = Me.Controls.Add("Forms.CommandButton.1", "CmdXYZ" & i, False)      With ctl_Command         .Left = 100         .Top = 100         .Width = 255         .Caption = "Click Me " & CStr(i)         .Visible = True     End With      ReDim Preserve cmdArray(1 To i)     Set cmdArray(i).CmdEvents = ctl_Command      Set ctl_Command = Nothing  End Sub 

and paste this code in a class module

Option Explicit  Public WithEvents CmdEvents As MSForms.CommandButton  Private Sub CmdEvents_Click()      MsgBox "Hello Word"  End Sub 

SNAPSHOT

enter image description hereenter image description here

like image 96
Siddharth Rout Avatar answered Nov 02 '22 01:11

Siddharth Rout


You need to add the code to the UserForm programatically. I used my code from this vbax article as the reference

The code below:

  1. Runs from a normal module
  2. Adds the button to a UserForm called UserForm1
  3. Adds this code to the Userform for a Click Event

    Private Sub CommandButton1_Click() Call Transfer End Sub 

VBA from normal module

    Sub AddToForm()     Dim UF As Object     Dim frameCOntrol1 As Object     Set UF = ActiveWorkbook.VBProject.VBComponents("UserForm1")     Set frameCOntrol1 = UF.designer.Controls.Add("Forms.CommandButton.1")     With frameCOntrol1         .Width = 100         .Top = 70         .Left = 10         .ZOrder (1)         .Visible = True         .Caption = "Transfer to Sheet"     End With      With UF.CodeModule         .InsertLines 2, _                      "Private Sub " & frameCOntrol1.Name & "_Click()" & Chr(13) & _                      "Call Transfer" & Chr(13) & _                      "End Sub"     End With  End Sub 
like image 25
brettdj Avatar answered Nov 01 '22 23:11

brettdj