Add code to a dynamically created Commandbutton

Kirnon

Board Regular
Joined
Apr 23, 2008
Messages
110
Hi,

Once again I hit a brick wall after having spent all morning trying to figure it out I come to you, MrExcel Forumites.

Below I present my code thus far - it all works as intended. What I need to now do is add the following line to the Command Button created in the code -
Code:
UserForm2.Show

Simple enough one would think. But due to the dynamic nature of the buttons name I am hitting a brick wall.

Please help.

Code:
Sub New_Applicant()
Dim shp As Shape
NewRow = Sheets("Applications").Cells(Rows.Count, 2).End(xlUp).Row + 1
OldRow = Sheets("Applications").Cells(Rows.Count, 2).End(xlUp).Row
Set Rng = Sheets("Applications").Range("A" & NewRow)
With Sheets("Applications")
    .Range("A" & OldRow & ":BM" & OldRow).Copy
    .Range("A" & NewRow & ":BM" & NewRow).PasteSpecial xlAll
    .Range("B" & NewRow & ",T" & NewRow & ",AL" & NewRow & ":AN" & NewRow & ",AP" _
                & NewRow & ":BM" & NewRow).ClearContents
    .Range("B" & NewRow & ":B" & NewRow) = UserForm1.TextBox1.Value
    .Range("J" & NewRow & ":J" & NewRow) = UserForm1.TextBox2.Value
    .OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=Rng.Left, Top:=Rng.Top, _
        Width:=Rng.Width, Height:=Rng.RowHeight).Select
    Set shp = ActiveSheet.Shapes(Selection.Name)
    With shp.OLEFormat.Object
        .Object.Caption = ""
    End With
    shp.Name = "Add" & NewRow
End With
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Code:
Sub New_Applicant()
Dim shp As Shape, NewRow As Long, OldRow As Long, Rng As Excel.Range
NewRow = Sheets("Applications").Cells(Rows.Count, 2).End(xlUp).Row + 1
OldRow = Sheets("Applications").Cells(Rows.Count, 2).End(xlUp).Row
Set Rng = Sheets("Applications").Range("A" & NewRow)
With Sheets("Applications")
    .Range("A" & OldRow & ":BM" & OldRow).Copy
    .Range("A" & NewRow & ":BM" & NewRow).PasteSpecial xlAll
    .Range("B" & NewRow & ",T" & NewRow & ",AL" & NewRow & ":AN" & NewRow & ",AP" _
                & NewRow & ":BM" & NewRow).ClearContents
    .Range("B" & NewRow & ":B" & NewRow) = UserForm1.TextBox1.Value
    .Range("J" & NewRow & ":J" & NewRow) = UserForm1.TextBox2.Value
    .OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=Rng.Left, Top:=Rng.Top, _
        Width:=Rng.Width, Height:=Rng.RowHeight).Select
    Set shp = ActiveSheet.Shapes(Selection.Name)
    With shp.OLEFormat.Object
        .Object.Caption = ""
    End With
    shp.Name = "Add" & NewRow
End With
[B][COLOR=#b22222][SIZE=2]With ThisWorkbook.VBProject.VBComponents(Sheets("Applications").CodeName).CodeModule
    .InsertLines .CreateEventProc("Click", "Add" & NewRow) + 1, "UserForm2.Show"
End With[/SIZE][/COLOR][/B]
End Sub


ps. you need to have Trust Access to the VBA project object model
 
Upvote 0
You sir, are a flippin' genius. That has done the trick wonderfully. except for one small hiccup. It keeps opening up the VBA project model which obviously I need to ensure doesn't happen for the end user. Do you know why this is happening and perhaps how to stop it?
 
Upvote 0
It looks like you are adding an ActiveX command button right above the cell rng.
You could:
1) Dispense with controls altogether. Instead of pressing a command button to trigger the routine, the user could double click the cell rng.

2) Use a button from the Forms menu, linked to a preexisting macro.
Code:
With rng
    With .Parent.Buttons.Add(.Left, .Top, .Width, .Height)
        .OnAction = "myMacro"
        .Visible = True
    End With
End With
 
Upvote 0
@mikerikson: That is a much better option than all the hoops I have been trying to jump through for this code.

Thank you for that.
 
Upvote 0

Forum statistics

Threads
1,215,844
Messages
6,127,245
Members
449,372
Latest member
charlottedv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top