VBA Create Command button and create/add new code to it

Mr Excelinator

New Member
Joined
Mar 4, 2014
Messages
37
Hi there chaps.

I'm trying to create a piece of code to do the following:

In a new workbook that has just been created from a Master report to 1. create a command button & 2. add a create piece of code (for example sort a column).

I need help 1 & 2 above.

The rationale for this is to create output reports to be sent to excel illiterate users, who will then have a series of buttons available to them so they can press to sort/subtotal the report in various ways.

Any help much appreciated.
Mr. Excelinator.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
My code currently is as such but falls down
Code:
Sub Create_PP_Button()
Dim Obj As Object
Dim Code As String
ActiveSheet.Select
'create button
    Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
    Link:=False, DisplayAsIcon:=False, Left:=200, Top:=100, Width:=100, Height:=35)
    Obj.Name = "PPButton"
'button text
    ActiveSheet.OLEObjects(1).Object.Caption = "Subtotal by Project Partner"
'macro text
    Code = "Sub PPButton_Click()" & vbCrLf
    Code = Code & "Call PP_Sort" & vbCrLf
    Code = Code & "End Sub"
'add macro at the end of the sheet module
    With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule
        .insertlines .CountOfLines + 1, Code
        
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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