Okay, I've got a strange problem. The macro below WAS (yesterday) working well to save the active workbook to a specific location with a specific name. Now, Excel shuts down when it hits that bit of code. Any idea what would be causing it? BTW, it tries to save to the location; there's an unknown type of fill deposited there, with a name like "7A08C000". If I put an XLS extension on it it says the file format is not valid.
If I change "ActiveWorkbook" to "TwoB" I get the same result. But, if I make a different workbook the active workbook, the macro works.
EDIT: I edited the post to attach a copy of the Lookups sheet.Sub Save() Set TwoB = Workbooks("2BDeleted.xls") Application.DisplayAlerts = False Dim MO As Variant, HO As Variant Set MO = TwoB.Sheets("Lookups").Range("H1") Set HO = TwoB.Sheets("Lookups").Range("M1") Stop ActiveWorkbook.SaveAs FileName:= _ "\\12AUST1001FS01\SHARE10011\Budget\SOBUDGET\CPS\MFR Projections\2011\Current\" & HO & " MFR Projection for " & MO & ".xls", FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False Application.DisplayAlerts = True End Sub
Last edited by jomili; 07-25-2011 at 12:15 PM.
Okay, I've done some more testing, and I can narrow down the problem even more.
The problem seems to be triggered by the HO and MO variants; for some reason Excel doesn't seem happy about those two. Any ideas on another way to do it?
I still need help.
I tried this morning just saving the active workbook with a new name.Same reaction, Excel shuts down and offers to recover my work.ActiveWorkbook.SaveAs HO & " MFR Projection for " & MO & ".xls", FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
Two thoughts spring to mind:
1. Use HO.Value and MO.Value
2. Don't call your sub Save as that is an Excel method
Romper,
Thanks for the advice, but no go. I tried putting .value after MO and HO, and still got the same reaction. "Save" was just a temporary macro, not the full routine, so I changed the name to "SaveIt". As a separate routine, if I just run it over and over, sometimes it will work, sometimes it won't. As part of the larger routine, it fails every time, but catastrophically, by shutting down Excel. I've pasted the larger routine below; it's a lot to go through, but maybe it will give you a clue as to what's happening.Sub Finish() 'Restore the play pivot to its former glory Run "Killbutts" 'Now back to the magic Set TwoB = Workbooks("2BDeleted.xls") 'Get the splash page back Workbooks("MFR Projection Tool.xls").Sheets("Sheet1").Activate With Application .ScreenUpdating = False .EnableEvents = False TwoB.Sheets("Play").Activate 'Copy the pivot to our Worksheet page as values and formats Set PT = ActiveSheet.PivotTables(1) PT.TableRange1.Copy TwoB.Sheets("Worksheet").Activate With TwoB.Sheets("Worksheet").Range("A1") .PasteSpecial Paste:=xlPasteValues .PasteSpecial Paste:=xlPasteFormats Range("A1").EntireRow.Delete 'Refigure our last row LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(-1, 0).Row 'Fill in the blanks in columns A through B with the value above Range("A3:B" & LastRow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C" 'Columns("A:B").Value = Columns("A:D").Value Columns("A:B").Copy Columns("A:B").PasteSpecial Paste:=xlValues, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False 'Create the lookup column Range("A1").EntireColumn.Insert 'Plug in the formula Range("A2:A" & LastRow).FormulaR1C1 = "=RC[1]&RC[2]&RC[3]" End With 'Done with the Worksheets sheet 'Now let's bring in the values from the Play pivot to our projections TwoB.Sheets("MFR Adjustments").Activate With TwoB.Sheets("MFR Adjustments") 'Refigure our last row LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(-1, 0).Row Range("E1").FormulaR1C1 = "Current MFR Projection" Range("E2:E" & LastRow).FormulaR1C1 = _ "=IF(ISNA(VLOOKUP(RC[-4]&RC[-3]&RC[-2],Worksheet!C[-4]:C[3],8,FALSE)),0,VLOOKUP(RC[-4]&RC[-3]&RC[-2],Worksheet!C[-4]:C[3],8,FALSE))" Range("F1").FormulaR1C1 = "MFR Adjustments" Range("F2:F" & LastRow).FormulaR1C1 = "=RC[-2]-RC[-1]" 'Find last column LastCol = Range("IV5").End(xlToLeft).Column With Application.WorksheetFunction 'Place column totals in row after current last row For iCol = 5 To LastCol 'Starting in column E Cells(LastRow + 1, iCol) = .Sum(Range(Cells(1, iCol), Cells(LastRow, iCol))) Next iCol End With Columns("A:F").EntireColumn.AutoFit Columns("D:F").Style = "Comma" End With 'Done with the MFR Adjustments sheet 'Now let's save this puppy where it needs to be Application.DisplayAlerts = False Dim MO As Variant, HO As Variant Set MO = TwoB.Sheets("Lookups").Range("H1") Set HO = TwoB.Sheets("Lookups").Range("M1") Run "SaveIt" ' TwoB.SaveAs FileName:= _ ' "\\12AUST1001FS01\SHARE10011\Budget\SOBUDGET\CPS\MFR Projections\2011\Current\" & HO.Value & " MFR Projection for " & MO.Value & ".xls", FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ ' ReadOnlyRecommended:=False, CreateBackup:=False 'Clean up Run "Delete2BDeleted" 'Start showing if there are any problems 'Application.DisplayAlerts = True 'Let Allison know it's ready Run "Sendmail" 'Now we save it to our desktop Application.DisplayAlerts = False Dim DTAddress As String Dim WshShell As Object Set WshShell = CreateObject("WScript.Shell") DTAddress = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator ActiveWorkbook.SaveAs DTAddress & ActiveWorkbook.Name Application.DisplayAlerts = True 'Return the focus to excel AppActivate "Microsoft Excel" MsgBox "The projection file has been saved to your desktop and to" & vbCrLf & vbCrLf & _ "S:\Budget\SOBUDGET\CPS\MFR Projections\2011\Current" & vbCrLf & vbCrLf & _ "Please review to identify variances and items of concern." Workbooks("MFR Projection Tool.xls").Close False 'We're done. End With 'this turns screen updating back on End SubSub SaveIT() Set TwoB = Workbooks("2BDeleted.xls") 'Now let's save this puppy where it needs to be Application.DisplayAlerts = False Dim MO As Variant, HO As Variant Set MO = TwoB.Sheets("Lookups").Range("H1") Set HO = TwoB.Sheets("Lookups").Range("M1") ActiveWorkbook.SaveAs HO & " MFR Projection for " & MO & ".xls", FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False Application.DisplayAlerts = True End Sub
Last edited by jomili; 07-25-2011 at 08:54 AM.
A couple of immediate questions/observations:
1. Why are you using Run rather than calling the code directly?
2. There's a lot of unnecessary selecting, activating and unqualified Range statements in there.
3. What are the values of HO and MO?
1) Self-taught in VBA, so blame my teacher that I don't know the right code to use. What do you mean by "Call directly", rather than "Run"?
2) You'd have to point out specifics for me to explain the "why"s of these.
3)TwoB is the workbook that contains the Lookups sheet I attached to my first post. HO and MO are in there.Dim MO As Variant, HO As Variant Set MO = TwoB.Sheets("Lookups").Range("H1") Set HO = TwoB.Sheets("Lookups").Range("M1")
1. I mean:
rather than:Killbutts
2. For example here:Run "Killbutts"
you would be better off explicitly stating which sheet is being used. The odd thing is that sometimes you do it and others you don't - such as in this part:Range("A3:B" & LastRow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
where you have two fully qualified Range references, one unqualified one, then one qualified only with Activesheet.With TwoB.Sheets("Worksheet").Range("A1") .PasteSpecial Paste:=xlPasteValues .PasteSpecial Paste:=xlPasteFormats Range("A1").EntireRow.Delete 'Refigure our last row LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(-1, 0).Row
3. That doesn't tell me what the values are.
Run or Call;
I just did some research, and it appears you use Run when you're calling a macro from another workbook, Call when you're running a macro within a workbook. All of my macros are run from one workbook, and they all modify another workbook, and use other workbooks. So that's why I use Run.
If the code is all in the same workbook, you don't use Run, regardless of which workbook is being modified. If you do use Run, you should specify the workbook that the code is in.
For 1), are you saying I could just put Killbutts, with nothing else, and "Killbutts()" would run?
For 2, you're right. I had thoughtwould cover all of the code under it until my "End With", but my WITH is only talking about A1, not the other cells. Sloppy of me; I'll work on that.With TwoB.Sheets("Worksheet").Range("A1")
For 3, I don't remember what values were in the workbook I first posted, as they change depending on what month we're in a what region we're running. Representative values are June for H1, 030 for M1.
1. Yes. You can also usebut it is unnecessary.Call Killbutts
Do you have any code in the BeforeSave event as a matter of interest?
No, nothing before save. Would it help if I posted the workbook that all of my code is in? A lot of it wouldn't run, as it depends on other workbooks, but you're welcome to inspect the code if you'd like.
Feel free - though if it's a lot of code I can't guarantee when I can look through it.
It's getting worse. When my active workbook (TwoB) is created, it's automatically saved to my desktop. So, I added a routine right before my attempted save to the new drive, to save the active workbook, no name change or anything. Excel crashed again, no error message other than Excel is attempting to save my work. The code I used to save my workbook is simply. I use it successfully in previous macros, but at the point in question it causes Excel to crash.ActiveWorkbook.Save
I've attached my workbook containing all of my code in the hope that someone can pinpoint the problem. As it stands now, ActiveWorkbook.Save works in the "Lookups" macro, but fails in the "Finish" macro. Any help is greatly appreciated.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks