Solved

Excel VBA - Entering code in Workbook_Open event for new workbook created by copying worksheets from original workbook

Posted on 2011-09-25
10
431 Views
Last Modified: 2012-05-12
My originating workbook has code that generates a new workbook by copying some of its worksheets: Sheets(WksNamesArray).Copy

I want to add code to the NEW workbook's Workbook_Open event that will prompt the user to save the workbook.  Is there a way to do that?

Thanks, --Andres
0
Comment
Question by:AndresHernando
  • 5
  • 5
10 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36597479
It would be easier to create a template with the required code in it, then simply create a new workbook from that template and copy the worksheets to that. Do you really want the prompt code to run every time they open it?
0
 

Author Comment

by:AndresHernando
ID: 36597534
Hi rorya,
I can't use templates; I need the original file to generate the new file.
What I intend to do, so that it will prompt to save only once, is to populate a "helper cell" that signals that the file has been saved.
Is there a way to put code in the new file's Workbook_Open event using VBA?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36597564
There is, but it is dependent on user settings that you can't alter.
So before we start, two questions:
1. Why can't you use templates? The original file can still generate the new file based on the template
2. Why do you want to prompt the user to do a Save As after they open the file for the first time? Why can you not prompt them for a location initially instead?
0
 

Author Comment

by:AndresHernando
ID: 36597857
rorya,
1) The reason I can't (or better said, would rather not) use templates is that there will be many users who will be downloading the originating file and, to minimize the complexity, I'd rather have them download a single file.

2) Prompting them for a specific location (like desktop?) will probably be OK.

Thanks, --Andres
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 36597891
For example:
Sub SaveFile()
   Dim wb As Workbook
   Dim varFileName
   Dim strDesktop As String
   
   ' copy sheet to new workbook - just for demo purposes
   ActiveWorkbook.Sheets(1).Copy
   Set wb = ActiveWorkbook
   ' get path to desktop
   strDesktop = CreateObject("WScript.Shell").specialfolders("Desktop") & "\"
   ' prompt for save file name
   varFileName = Application.GetSaveAsFilename(strDesktop & wb.Name)
   ' save
   If varFileName <> False Then wb.SaveAs varFileName
End Sub

Open in new window


Would that work?

Regards,
Rory
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:AndresHernando
ID: 36598193
Rory, your code would work with one exception:  the filename is blank (waiting for the user to enter a filename).  I need it to show the existing filename.   I tried tweaking your code but couldn't make that happen.

Thanks, --Andres
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36598245
The file name should not be blank (it isn't in my tests) as it uses wb.name to pass the existing file name. If you want to use the name of the file the sheets came from, then you will need to pass that instead.
0
 

Author Comment

by:AndresHernando
ID: 36598474
Rory, So you can see what is happening, I'm attaching the file that is generated and meant to be emailed to the recipient.

Thanks, --Andres Approval-Unit---PPR-FLR-11-0005-.xlsm
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36598552
I don't think I'm seeing what you are, as I see the file name in the save box.
I would however change the getsaveasfilename to add the file filter:

   varFileName = Application.GetSaveAsFilename(strDesktop & wb.Name, "Microsoft Excel files (*.xlsm), .xlsm")

Open in new window

0
 

Author Comment

by:AndresHernando
ID: 36708192
Rory,  as usual, you have been a huge help.  Got it working fine now.

--Andres
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run macro only on one sheet within workbook 13 67
iPhone excel activation issues 11 66
Excel 2016 formulas 5 24
Help with Excel formula 6 35
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now