How to embed commandbutton code within the button

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
655
Hello Again,

I have a macro that creates a new sheet, formats the entire sheet, adds all of the formulas throughout the sheet, and creates a few buttons.

These Buttons (Reset Sheet, Print, Save, and Index) aren't complicated, but because I am constantly generating a new sheet, I was wondering if there was a way to embed the "call" part into the button when it's made otherwise generating a new sheet would then require someone to go into the sheet's coding and put the command in there.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Won't let me delete but I solved.....

For the next guy:

Code:
ActiveSheet.Buttons.Add(x,x,x,x).Select 'Coordinates of the Button
Selection.OnAction = "YourMacroToRun"
 
Upvote 0
Why not create a template sheet.

Have all the buttons and formatting the way you want.

And then when you want a new sheet make a copy of the template


Try this:
Code:
Sub Copy_Template()
'Modified  11/5/2018  3:57:41 PM  EST
Sheets("Template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Template" & ActiveSheet.Index
End Sub

Then modify the sheet name like you want
 
Last edited:
Upvote 0
Templates might work-

The workbook basically lays out like this- I have five sheets+

1. Notes- This has constants in it, like mileages and such, that never change. They are used throughout the other sheets.
2. Voyage Specifics- This has the constants in it particular to one voyage. This of a simple algebraic formula, =Ax^2 + bx + c. If this formula is used throughout the last three sheets (which is what the workbook is based around), the c for each of these formulas comes from this sheet.
3. The "Noon" sheet- 1 of the three important sheets in the book- this is the printable "report" for the day's stats. The entire workbook is based around printing this report. The "Noon" is the first day.
4. The "Noon#" sheet- Although not always used, this is the "Noon" Sheet plus a day (or more). If a voyage is 5 days, there is the original "noon" plus Noon 2, Noon 3, Noon 4, and Noon 5. Each day builds upon the last or subtracts from the last (i.e. total hours builds, and distance to go subtracts).
5. The "Arrival" sheet- the last and always used of the three reports- this gives the totals for the voyage. It also includes the "noon" info from the last "noon" up until now- meaning the "noon" gives voyage info every 24 hours and the arrival gives the totals but also info from the last noon until the termination (less than 24 hours) of the voyage.

The noon is used 99.9% of the time- occasionally not. The Noon# is usually used but the # of them ranges often- my old workbook, prior to VBA, used a lot of IF formulas and I had Noon-Noon 10 of dedicated sheets- usually didn't use more than 10 and would have leftovers but if I did use more than 10 it was screwed.
 
Upvote 0
Glad to see you previously said:
Won't let me delete but I solved.....

 
Upvote 0
No worries-

While I'm thinking-

Do you know the coding for a "Save As" button that will "save as" the workbook with a new name, defined in sheet1, cells A1:A5 (5 different parts of a name), delete the old workbook, and archive the file in a specified folder- if the folder won't exist then to create it.

Basically looking to have a blank version of the workbook named "Master Template" First time you use it it gets saved as "Current Voyage Report (I could make this with a specified directory), but I want it to save as in a new directory and archived name when the "arrival sheet" is finished (now you see why I have three types of sheets and buttons). The directory, specified in sheet 1, cell B22, is C:\Users\sassr\desktop\Noons\350-400 351-400 is the folder for voyages 350 through 400. there are also 7 other folders, 1-50, 51-100, etc etc. I'd like this whole piece of code to create a new folder (i.e. 401-450 when the time comes).
 
Upvote 0
You said:
No worries-

Not sure what that means. I was just saying it sounds like you already found a way to do what you wanted.

As far as your last question. I would suggest you start a new posting and post this question.

This is beyond my knowledgebase.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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