MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 21st, 2002, 07:02 PM   #1
Boozer
Board Regular
 
Join Date: May 2002
Posts: 52
Default

I have written some code that saves a different workbook based on date. However, the workbook that I am trying to save has its own beforesave code in it. When I run my code to save the specified workbook, it runs it beforesave procedure, as it should. But the code in that procedure has no effect. It won't select cells or unprotect the worksheet or activate different sheets. I tried remarking out all the code in the beforesave procedure, so that the procedure was called, but didnt do anything. When I did this, my SaveAs command would execute, but would not actually save the workbook. Any suggestions?
Boozer is offline   Reply With Quote
Old May 21st, 2002, 07:06 PM   #2
Colo
MrExcel MVP
 
Colo's Avatar
 
Join Date: Mar 2002
Location: Kobe, Japan
Posts: 1,420
Default

Hi Boozer,

Could you show us your code?
__________________
Regards,

Masaru Kaji aka Colo - Ex Microsoft MVP Since 2004 -2009
Colo is offline   Reply With Quote
Old May 21st, 2002, 07:30 PM   #3
Boozer
Board Regular
 
Join Date: May 2002
Posts: 52
Default

How's this for example. BTW I am using Excel '97.
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/21/02 by NCC
' This is the main macro

'
ActiveWorkbook.SaveAs FileName:="C:WINNTProfilesnccDesktopHelp.xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

End Sub

Before it saves the file, the following BeforeSave code runs, but will NOT select Sheet2 to put the value in. The "help" will be placed on Sheet1 in A1.The code works by itself, just not in conjuction with the SaveAs code in the macro.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet2").Select
Range("A1").Value = "help"
End Sub
Boozer is offline   Reply With Quote
Old May 22nd, 2002, 03:45 PM   #4
Boozer
Board Regular
 
Join Date: May 2002
Posts: 52
Default

Any ideas?
Boozer is offline   Reply With Quote
Old May 22nd, 2002, 03:52 PM   #5
brettvba
MrExcel MVP
 
Join Date: Feb 2002
Location: Christchurch New Zealand
Posts: 1,030
Default

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet2").Range("A1").Value = "help"
End Sub

try that there is no reason why the sheet should not be selected
if this doesn't help post back
brettvba is offline   Reply With Quote
Old May 22nd, 2002, 06:03 PM   #6
Boozer
Board Regular
 
Join Date: May 2002
Posts: 52
Default

Okay, that puts the "help" on sheet2, but unfortunately, I did not write my previous code that way I didn't think that by adding SaveAs code to the project that I would have to rewrite my all my previous code. Could someone tell me why, by adding the SaveAs code, the BeforeSave code will not activate the sheet or protect/unprotect the active workbook (EX. ActiveSheet.Protect has no effect in the BeforeSave code if this code is called by the SaveAs code)?
Boozer is offline   Reply With Quote
Old May 22nd, 2002, 07:32 PM   #7
brettvba
MrExcel MVP
 
Join Date: Feb 2002
Location: Christchurch New Zealand
Posts: 1,030
Default

HI,
Don't really understand what your asking sounds like a new question if your code is not working post it and I will have a look!.
brettvba is offline   Reply With Quote
Old May 22nd, 2002, 08:04 PM   #8
Boozer
Board Regular
 
Join Date: May 2002
Posts: 52
Default

What I am trying to say is... Lets say you have the following code in Excel '97 that simply saves a file.

Sub Problem()
ActiveWorkbook.SaveAs FileName:="C:\WINNT\Profiles\ncc\Desktop\Help.xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

End Sub

Now that works fine and dandy, UNLESS you have BeforeSave code in the workbook such as this:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet2").Range("A1").Value = "help" ' This works and puts "help" in cell A1 on sheet2

Sheets("Sheet2").Select
Range("A1").Value = "help"
'the previous two statements should work the same, but sheet2 is not selected and "help" is placed on sheet1

ActiveSheet.Protect 'This has no effect on the sheet in the BeforeSave macro. The sheet is not protected, and if you try to unprotect a protected worksheet, that does not work either. If one steps through the code, it passes through the code like it worked correctly, but if you go look at the sheet, it is not protected/unprotected (whichever one is trying to do).

End Sub

I only encouter this problem when a SaveAs code is combined with BeforeSave code. The SaveAs code works fine by itself. And the BeforeSave code works fine by itself. But throw the two together and the BeforeSave code seems to have almost no effect.
Boozer is offline   Reply With Quote
Old May 22nd, 2002, 08:31 PM   #9
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

I'm guessing that your BeforeSave event is not firing because the SaveAs creates a new workbook and does not save the calling workbook. The before save event is a workbook level event. If the workbook is newly created, it is not going to fire the event in the calling workbook. You will need to capture this event at the application level. In order to do this, you will need to create an application class of your own. Please see this link for a better explanation of using application level events. I already tested it for your purposes and it works fine.

http://www.cpearson.com/excel/AppEvent.htm

Tom
Tom Schreiner is offline   Reply With Quote
Old May 22nd, 2002, 09:42 PM   #10
Boozer
Board Regular
 
Join Date: May 2002
Posts: 52
Default

So do I need both the BeforeSave and SaveAs code at the Application level?
Boozer is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 01:08 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes