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 16th, 2002, 08:33 AM   #1
nancyo
Board Regular
 
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 255
Default

I have a macro which copies a worksheet from one excel workbook into a new workbook and saves as a new filename.

I modifed the code (message box) to allow for the user to select NO and stop the macro. Works fine.

The problem occurs when the user runs the macro a second time (may happen on rare occassions). An excel message box appears which states that the file already exists, and do we want to continue. If the user selects YES, the macro runs fine.

If the user selects NO or CANCEL, get a run time error '1004 . The section of the code for saving the file is highlighted.

I tried to modify the code to account for this, trying to add IF THEN statements to account for this messagebox. The problem is that the message box is NOT part of the code, but a function of excel.

Now, I can live with this, as only 3 people will be using this particular macro (I'm one of the 3), and I can show them what to do.

But, the AR part of me wants to have the macro run for all scenarios.

Any suggestions / comments?
nancyo is offline   Reply With Quote
Old May 16th, 2002, 09:02 AM   #2
Joe Was
MrExcel MVP
 
Joe Was's Avatar
 
Join Date: Feb 2002
Location: Central Florida, USA
Posts: 7,541
Default

Did you remember to de-activate and re-activate:

Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False

your code here!

Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

Hope this helps? JSW
Joe Was is offline   Reply With Quote
Old May 16th, 2002, 09:22 AM   #3
nancyo
Board Regular
 
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 255
Default

Joe Was - That's it!!!!

Now the message does not appear, so I am going to add more to my initial mesage box to let the use know that they can over-write a file.

SO EASY!!!!!!!!!! My god.....THANKS!!!!!!!!


nancyo is offline   Reply With Quote
Old May 16th, 2002, 09:24 AM   #4
nancyo
Board Regular
 
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 255
Default

Double post - oops! In my excitement...!


[ This Message was edited by: nancyo on 2002-05-16 08:25 ]
nancyo is offline   Reply With Quote
Old May 16th, 2002, 09:47 AM   #5
Adiv
New Member
 
Join Date: May 2002
Posts: 7
Default

If you're getting a 1004 (Application-defined or object-defined error) you should address it and not cover it up. Can you display your subroutine? You shouldn't let Excel generate an alert about an existing file. You should use the Dir function to handle that situation proactively.

Just my $0.02
Adiv is offline   Reply With Quote
Old May 16th, 2002, 10:08 AM   #6
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Nancy, if you want to give the user the option to cancel on duplicate, you can use an error trapper like below:


Sub fsy()
Application.ScreenUpdating = False
fname = Sheets(2).[a1].Value 'set filename as cell a1's value on 2nd sheet
Sheets(1).Copy 'Copy first sheet to a new workbook
ChDrive ("C") ' drive letter
ChDir ("c:my documents") ' target directory
If fname <> "" Then 'make sure fname is not blank
On Error GoTo 1
ActiveWorkbook.SaveAs FileName:=fname & ".xls"
Else: MsgBox "Please Enter a value in A1 and retry."
End If
End
1: ActiveWorkbook.Close False 'close new workbook, don't save
End Sub

__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old May 16th, 2002, 10:55 AM   #7
nancyo
Board Regular
 
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 255
Default

NateO - I will try this code this afternoon, not familiar with the format.

The modification to my code with the alerts is an "easy" solution, which does work, but I would like to learn how to code it.

Adiv - what is the dir function?

[ This Message was edited by: nancyo on 2002-05-16 09:56 ]
nancyo is offline   Reply With Quote
Old May 16th, 2002, 11:35 AM   #8
Joe Was
MrExcel MVP
 
Joe Was's Avatar
 
Join Date: Feb 2002
Location: Central Florida, USA
Posts: 7,541
Default

Nancyo,
You need to test your options if you de-activate the alerts. Your code may not do what you think!
If the Excel warning is just to let you know that you are over-writing then the Alert cancel is the right way to go. If you cancel your save code and your file handling works the way you want, even with a sloppy cancel condition, you can keep the cancel alert. As the cancel alert will exit your code even if it has a problem. In this case the problem does not matter?

If the code does not do what you want then the cancel alert will hide the problem not fix it. With out seeing y6our code we cannot provide the correct exit for your option.

You may or may not have a problem with the way your application works when using the cancel alert, but your code could still be sloppy. If it works only you will know its sloppy!

Repost if this bothers you with your code and we will try to sort it out. Hope this helps. JSW
Joe Was is offline   Reply With Quote
Old May 16th, 2002, 12:15 PM   #9
nancyo
Board Regular
 
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 255
Default

Here is my code, just a couple of comments:

1. I have some of this from suggestions, and some from actually recording.

2. The first part, up to the copy, is generating a message box which gives the user a choice of OK or cancel. If OK, then the code copies and saves. If cancel, then displays a different message and ends.

3. The copy part of the code was recorded (removes all formulas and replaces with values, to eliminate links).

4. This code is set up without the excel alerts, so the initial message box specifies this. The only thing this alert does is let the user know the file already exists. Only Management will be using this particular macro, so we will know if over-writing would be a problem (hardly ever).

5. If I can change the code to inlcude Nate's stuff, I can, if it makes it more professional and accurate.

Sub TCtoApproved_4()
'To automatically copy/save a new file in approved excel directory with TCs only
Application.ScreenUpdating = False 'disables "flashing" screen
Application.DisplayAlerts = False 'disables excel alerts
Dim Msg, Style 'allows choice of OK or CANCEL
Msg = "To Automatically Generate TYPICAL COUNTS In The Approved Excel Directory - OK" & vbCr & vbCr & "Warning - This Macro Will Over-Write Any Existing File Each Time It Is Run"
Style = vbOKCancel + vbExclamation 'allows typical counts to be generated or canceled
response = MsgBox(Msg, Style)
If response = vbOK Then
fname = ActiveSheet.[F17].Text
Range("A1:I52").Select
Selection.Copy
Workbooks.Add
Range("A1").Select
ActiveSheet.Paste
Range("F17").Select
Selection.Copy
Range("F17").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("D24:D33").Select
Application.CutCopyMode = False
Selection.Copy
Range("D24").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("F24:F33").Select
Application.CutCopyMode = False
Selection.Copy
Range("F24").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveWindow.SmallScroll Down:=21
Range("F52").Select
Application.CutCopyMode = False
Selection.Copy
Range("F52").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("I52").Select
Application.CutCopyMode = False
Selection.Copy
Range("I52").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ChDir "N:TYPCNTapprovedLATFLOWbetaSLBL"
ActiveWorkbook.SaveAs Filename:=fname & ".xls"
ActiveWorkbook.Close
Range("a1").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Else 'typical counts NOT generated
MsgBox "Typical Counts Not Generated", 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True 'enables excel alerts
End If
End Sub



I don'tknow how to fix the format here...
[ This Message was edited by: nancyo on 2002-05-16 11:16 ]

[ This Message was edited by: nancyo on 2002-05-16 11:17 ]
nancyo is offline   Reply With Quote
Old May 16th, 2002, 01:26 PM   #10
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

The code below will allow for a cancel. Not sure what you mean by Dir, chDir is to change your active Excel directory. On my LAN, I have to change drives first or it's error city. They're both professional, depends what you want, if you want to allow a user to cancel change:

ChDir "N:TYPCNTapprovedLATFLOWbetaSLBL" 
ActiveWorkbook.SaveAs Filename:=fname & ".xls"
ActiveWorkbook.Close


to

ChDir "N:TYPCNTapprovedLATFLOWbetaSLBL"
On Error Resume Next
ActiveWorkbook.SaveAs _
Filename:=Application.Substitute(fname, ".xls", "") & ".xls"
on error goto 0
ActiveWorkbook.Close False


_________________
Cheers, NateO

[ This Message was edited by: nateo on 2002-05-16 13:37 ]
NateO 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 10:45 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