Inserting a sub into a new page using VB/Macros

Kabasauls

New Member
Joined
Jul 12, 2012
Messages
24
Hello,

I wrote a code that creates new pages based on a template. I found that I need to add three Subs() to every page that is created using this code. I have never done this before and after seraching for an answer, came out empty handed.

I am looking for a way to create a page and add code using my previously written code.

This is what I've tried to come up with so far.

Code:
Sub AddCode()

Dim sCode As String

   sCode = "sub VBAMacro()" & vbCr & " msgbox ""VBA Macro called"" " & vbCr & "end sub"
   'find a way to insert this code (String) into new page
      
End Sub

Thank you for your time.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
What do these subs do?

Are they event subs like Worksheet_Change/Worksheet_SelectionChange?
 
Upvote 0
There are three subs. The first one just call a form, the second one calls a function and the last one is a "Worksheet_BeforeDoubleClick" sub that calls a form.
 
Upvote 0
You might not need to add the 3 subs to each page.

For the BeforeDoubleClick event you can use the workbook event code SheetBeforeDoubleClick.

If you are calling the other 2 subs from Form buttons you can just have the subs in a standard module and assign them to the buttons.
 
Upvote 0
Thank you for that tip! I didn't understand the second part. I changed one of the subs to "Thisworkbook" and it's perfect.

This is the code that I have leftover to copy on the new pages. How can I add this code to the sheet every time is created?

Code:
Private Sub Print_Click()

     PopulateFrm.Show

End Sub

     Private Sub NewPage_Click()

Call NewPg

End Sub

Thank you for your time.
 
Upvote 0
You can't just move the double click code to the ThisWorkbook module.

It needs to be altered to use the workbook event SheetBeforeDoubleClick.

As for the other 2 subs, are you using buttons to call them?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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