473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HELP!!! Problem saving file in excel

63
I am running a macro from an excel file called "MASTER - FX Reconciliations Modelvista.xls" (excel 2010 in compatibility mode to save in 2003 format) from a button in the spreadsheet. The macro takes data from a file called "rollifrp.xls" (after a small amount of manipulation) and pastes it into the
"MASTER - FX Reconciliations Modelvista.xls" file. It then attempts to save the rollifrp file down with a new name dependent on the content of cells E4 and E6 on the Index tab of the "MASTER - FX Reconciliations Modelvista.xls" file. This renaming and saving is NOT successful - I get a run time error 1004 "document not saved". When I then debug and choose the run to cursor option and run the macro to the end it does exactly what I want it to. Why do I get the run time error and how can I get rid of it - it is driving me mad!! I've checked out the microsoft site but the content did not seem relevant. The code is below - any help greatly appreciated!


Expand|Select|Wrap|Line Numbers
  1. Sub FXcurr()
  2.  
  3.     Application.ScreenUpdating = False
  4.  
  5.     Dim stDocName As String
  6.     Dim stDocName2 As String
  7.     Dim AP As Range
  8.     Dim CurrYr As Range
  9.  
  10.     Application.ScreenUpdating = False
  11.  
  12.     Set AP = Worksheets("Index").Range("E4")
  13.     Set CurrYr = Worksheets("Index").Range("E6")
  14.  
  15.   ' rollifrp excel details only
  16.  
  17.     stDocName = "Q:\Reports\Financial Reporting\IFRS\Models\rollifrp.xls"
  18.  
  19.     Workbooks.Open stDocName
  20.  
  21.     Windows("rollifrp.xls").Activate
  22.     Cells.Select
  23.         With Selection
  24.         .HorizontalAlignment = xlCenter
  25.         .VerticalAlignment = xlCenter
  26.         .WrapText = True
  27.         .Orientation = 0
  28.         .AddIndent = False
  29.         .ShrinkToFit = False
  30.         .MergeCells = False
  31.     End With
  32.  
  33.     Rows("1:10").Select
  34.     Selection.Delete shift:=xlToUp
  35.  
  36.     Columns("F:F").Select
  37.     Selection.Delete shift:=xlToLeft
  38.  
  39.  
  40. '   Copy data to master file
  41.  Range("A:L").Select
  42.     Selection.Copy
  43.     Windows("MASTER - FX Reconciliations Modelvista.xls").Activate
  44.     Sheets("rollifrp").Select
  45.      Range("A1").Select
  46.  
  47.     ActiveSheet.Paste
  48.     Application.CutCopyMode = False
  49.  
  50. '   Reformat original file for saving in AP folders
  51.     Windows("rollifrp.xls").Activate
  52.  
  53.     Sheets("Sheet1").Select
  54. ActiveSheet.Range("A:L").Select
  55.  
  56.     Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
  57.         OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
  58.          Range("A1").Select
  59.  
  60.  
  61.     If AP.Value < 10 Then
  62.         stDocName2 = "Q:\Reports\Financial Reporting\IFRS\" & CurrYr & "\AP0" & AP.Value & "\04_Models\rollifrp AP0" & AP.Value & ".xls"
  63.     Else
  64.         stDocName2 = "Q:\Reports\Financial Reporting\IFRS\" & CurrYr & "\AP" & AP.Value & "\04_Models\rollifrp AP" & AP.Value & ".xls"
  65.     End If
  66.  
  67.  
  68.  
  69.  
  70. ActiveWorkbook.SaveAs stDocName2
Aug 18 '14 #1
10 1501
twinnyfo
3,653 Expert Mod 2GB
Grego9,

First, please use the code tags when posting your code, it makes it easier for us to follow.

Second, I am assuming the error actually fires on your last line of code:

Expand|Select|Wrap|Line Numbers
  1. ActiveWorkbook.SaveAs stDocName2
Is this correct?

My initial thought is that the file name (particularly the path), does not currently exist. You must make the directory into which you are trying to save the file, first. If the directory exists when you try to create it, you will get an Error 13, but if you trap that particular error, and resume next, all should work fine.

In your code you will need the following in strategic points:

Expand|Select|Wrap|Line Numbers
  1. Dim strPath As String
  2.  
  3. strPath =  "Q:\Reports\Financial Reporting\IFRS\" & _
  4.     CurrYr & "\AP0" & AP.Value & "\04_Models\"
  5.  
  6. OR...
  7.  
  8. strPath = "Q:\Reports\Financial Reporting\IFRS\" & _
  9.     CurrYr & "\AP" & AP.Value & "\04_Models\"
  10.  
  11. MkDir strPath
This should get you headed in the right direction (I hope).
Aug 18 '14 #2
grego9
63
Thanks for the reply. However, the directory already exists and the code can be executed fine when I simply go to debug and set run to cursor to the last line of code. It does everything I want it to - the only thing I don't want it to do is pop up the run time error when running the macro each time!

Thanks!
Aug 18 '14 #3
twinnyfo
3,653 Expert Mod 2GB
Hmmmmmmmmmmmm, very strange.

In my Excel automation, I've not run across a problem "Saving As..." My Excel manipulation is always done from MS Access, but the VBA should be nearly identical.

From a troubleshooter's perspective, I would try to open, manipulate and save some test files from within the same code, to determine if it is a problem with the files you are using or the code. Sometimes when I use very simple code to work with test files, I uncover a particular point elsewhere in my code that "appears" to work fine, but causes problems like this.

I wish I had a better answer, but your code looks to to fine.
Aug 18 '14 #4
grego9
63
I think the problem is regarding which sheet is active. If I put
ThisWorkbook.Activate before ActiveWorkbook.SaveAs StDocName2 the code saves the "MASTER - FX Reconciliations Modelvista.xls" with the right name and in the right location - However it is the rollifrp.xls file that I want saved down. A bit strange as I have the
Windows("rollifrp.xls").Activate
command in the code. Do you think this would be best placed in the Excel forum rather than VBA?
Thanks for looking at this for me.
Aug 18 '14 #5
twinnyfo
3,653 Expert Mod 2GB
I think you've hit on one of the challenges I've run across with automating Excel, too. Sometimes it is difficult to keep track of which file the system wants to work with.

Try "selecting" the workbook you wnat to save first:

Expand|Select|Wrap|Line Numbers
  1. Sheets("Sheet1").Select
And then try the SaveAs again.

This thread could go in either the Excel Forum or VBA. Excel (and all MS Office) "macros" are essentially VBA. Your "VBA" question deals specifically with MS Excel. But, I will let the moderators choose which forum best capturees the nature of your question.
Aug 18 '14 #6
grego9
63
Thanks - I've tried that and it still falls over. This code works elsewhere in the same workbook - bit of a puzzler. I've even tried picking up a different file and then saving that down instead - but it still falls over!
Thanks for your persistence on this one!
I might have to take a break before I go insane!
Aug 18 '14 #7
twinnyfo
3,653 Expert Mod 2GB
Have you tried closing the other file (MASTER - FX Reconciliations Modelvista.xls), first?

But, I feel you--the more time I work with VBA, the more trips I must make to the asylum. :-)
Aug 18 '14 #8
grego9
63
I re-engineered the solution. I copied the contents of the rollifrp.xls into the Master spreadsheet and then "copied" that tab to create a new file with the naming convention outlined above. Worked a treat. Shame I couldn't figure out how to address the original problem - but at least I will sleep at night now.
Aug 18 '14 #9
grego9
63
Thanks for your help twinnyfo - you got me thinking differently about how to solve it!
Aug 18 '14 #10
twinnyfo
3,653 Expert Mod 2GB
Glad I could be of service. Sometimes the solution is not he "right way" but at least solves the problem. let us know if we can help with anything else.
Aug 18 '14 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Létező | last post by:
Hi Python Fans! Could you send me a download URL of a compiled HTML Help (CHM) file for Python 2.2.3 ? Thanks: Complex
1
by: Helmut Blass | last post by:
hi Access-freaks, in MS Access, I generate an Excel file where the user should have the possibility to determine the excel file name by file dialog. so I use the following command: ...
7
by: alwayssmiling | last post by:
Hi frends, In my application, a class supports backend processing. In this class im creating a new excel sheet and im inserting some data into this excel sheet, and im trying to save the file...
1
by: NBKMNDN | last post by:
Hi frends, In my C# application im creating a new excel sheet and im inserting some data into this excel sheet, and im trying to save the file with the help of method SAVE AS. But it doesnot...
2
by: radraq | last post by:
Hello, I have just completed a survey in excel which I've uploaded to my website. The propblem is I would like the excel file to open directly online without asking for paswords and save the...
4
by: Sutharsan Nagasun | last post by:
Hi, I am new to Perl. I need help with file search for the following scenario. Currently as part of the archiving process, we have archived the files under /$rootdir/Archive/yyyy directory where...
5
by: JohnLorac | last post by:
Hello, can somebody help me with saving file into local disk using javascript? I made some sample code which unfortunately won't work :(. Applet sample file: public class IO extends...
0
by: Hema Suresh | last post by:
Hi all I developed a database using vb6 code I passed all the data to an excel file through code And I saved and closed the Excel application in the name "D:\Books Database.xls" via vb6 code ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.