Insert a macro in an about-to-be-created workbook

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,934
Office Version
  1. 365
  2. 2010
I have a macro that spins off a specific sheet from a workbook and saves it in a given path.
Before saving it, I'd like to have a macro placed in this about-to-be-saved workbook.
I have the relatively short code for the macro to be place in it, just don't know what I should do to go about this association with the new workbook.
 
Is there any reason that you cannot run the code from within the worksheet? There are only a few exceptions that I can think of where you MUST run code from a standard module. The target workbook only has a single worksheet. Right?
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Right. I was (unfortunately) dreaming about it last night and could put the macro in the reformatted sheet before it's saved.
Is there something special I need to do to save it as an XLSM workbook rather than an XLSX?
 
Upvote 0
I have the code written that needs to be in the reformatted sheet, but I don't know how to save that sheet as a separate XLSM file. Currently, I save it as an XLSX file using:

zNewWB.SaveAs sSaveAsFilePath

And "sSaveAsFilePath" has an extension in it that's "XLSX"...I tried to adjust it to "XLSM" but the saving failed.
 
Upvote 0
OK. I implemented Logit's suggest and that worked perfectly to save the new sheet as a workbook XLSM.
My remaining question is how, from the original macro code, do I put this new macro in the only sheet of the new workbook?

Phew! Thanks for remaining on top of this for me.

EDIT: Never mind...I got it! YAY!!!


Kevin
 
Last edited:
Upvote 0
Spoke too soon...All's good with one exception. I need this new macro to run with a shortcut key.
Not sure where that code should go. I think the code should be: Application.Onkey "+^{C}",InsertCopyRow
 
Upvote 0
I almost have it. However, when I put the new macro on the sheet that's being saved (it's called "TimeSheet"), the sheet is actually renamed before saving to contain a time stamp.
It seems that the Workbook_Open (with the shortcut key) and the macro that's supposed to do the copy and insert are both missing in the newly named workbook when saved.
Actually, upon further examination, they're not missing...they're on the sheet. However, the Workbook_Open event doesn't seem to be firing when I open the file, so the shortcut isn't working.

I'm getting a message that the macro isn't there (the macro being called by the Workbook_Open event).
 
Last edited:
Upvote 0
I believe I found the problem but don't know how to fix it. The Workbook_Open event needs to be in "This Workbook" but I don't know how to get it into that workbook before the book is saved.
 
Upvote 0
Pulling my hair out. I put Auto Open() in the sheet that becomes the workbook.
The macro to copy and insert was placed in the TimeSheet. However, it seems that when I placed it there, it got date-stamped.
So, even though the Auto Open sets the shortcut, it's using the old date-stamp for the macro so it can't find it.
OY.
 
Upvote 0
Unfortunately, Auto Open() is one of the exceptions that require a standard module. :)
With new factors in play, another option is to have a destination workbook that contains the code before-hand. You will copy the formatted worksheet to this destination workbook, and save a copy of the destination workbook. This can be done automatically with code as long as you know where the destination workbook is located. Another option is to create an add-in. Both are better than using the extensibility library IMO. Please post all of your relevant code and requirements..
I'll throw together an example that you can work with. Don't pull out your hair or you'll look like me. o_O
 
Upvote 0

Forum statistics

Threads
1,216,016
Messages
6,128,299
Members
449,437
Latest member
Raj9505

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