+ Reply to Thread
Results 1 to 17 of 17

Error 1004 when creating new workbooks

  1. #1
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Error 1004 when creating new workbooks

    Hello,

    I am in the process of revising a complex macro that I wrote to translate energy e-bills into a standard format for database import. As part of its initialisation, the macro creates and saves three standardised workbooks: one for bills with a single unit rate, one for bills with a dual unit rate, and one for bills that do not match an existing record in our database. Upon saving the first of these workbooks (line highlighted in red below) the macro generates an Error 1004: application-defined or object-defined error.

    I have attached the complete macro - though you won't be able to run it, as I cannot for privacy reasons send you an e-bill to use with it - and the relevant section of code at the bottom of the post.

    Thank you in advance for your help.

    Notes:

    1) While the error is generated by the highlighted line, all lines in the box are performed. This happens even when advancing stepwise in the VB editor.
    2) Despite generating the error, the workbook is still properly created and saved with the correct filename.
    3) No revision has been made to this section of code, but it worked perfectly in the original version of the macro.
    4) The original macro also generates Error 1004 when opening the source workbook. The source workbook filename is "EDF work file original.xls" and its path is valid.
    5) The revised macro generates Error 1004 when opening a valid source workbook that has worked with the original macro.

    Please Login or Register  to view this content.
    EDF NHH macro WIP.xls

  2. #2
    Forum Contributor
    Join Date
    08-01-2012
    Location
    Tampa
    MS-Off Ver
    Excel 2010
    Posts
    121

    Re: Error 1004 when creating new workbooks

    You need to specify the entire path to where you want to save, as well as the actual filename and extension. For Example:

    Please Login or Register  to view this content.
    If not outputting to a network share, I would get the path to the current user's desktop or another special folder, so that anyone else running the macro wont encounter a problem.

  3. #3
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Error 1004 when creating new workbooks

    Jay, thanks for your quick response. Unfortunately your solution doesn't resolve the issue. If I put the path where you suggest putting it, there is no change - I get Error 1004 and the code continues running to the end of the sample.

    If instead I put the path directly into sRateFile or OutFile, or change the initial definition of OutFile to include the full path, the same line now returns Error 9 (Subscript Out Of Range) and the code stops after creating and saving ErrorFile.

    UPDATE: I just tried setting the filename (including path) manually instead of using SRateFile, and still got the Error 9. I was pretty sure it wasn't the variables before because the files still created, but now I'm 100% certain it's not that. What does that leave?
    Last edited by Jedit; 07-18-2013 at 07:24 AM.

  4. #4
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Error 1004 when creating new workbooks

    Further update: I have been experimenting with moving original and test data and macros into each other's home directories. I obtained the following results:

    (NB: "original" is used throughout to refer to the original version of the macro, its home directory and associated data. "Test" refers to the macro as listed in my OP, its home directory and associated data.)

    1) Using the original macro on test data in the original directory, the file creation section worked fine without my having to add in the path.

    2) When I reversed the process and copied the original macro into the test directory, the macro returned Error 1004 during file creation when used with either test data or original data that had worked in the original directory.

    3) Copying the test macro into the original macro's directory returned Error 1004 when used with either test or original data.

    4) Adding the path to the original macro causes it to return Error 9 regardless of location.

    So, I have fundamentally identical code returning different results based on which spreadsheet it is in and where that spreadsheet is located.

    I have uploaded a copy of the original macro for comparison, but the only difference is which cells are reformatted.

    Scottish Hydro bill translation macro v5.xls

  5. #5
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Error 1004 when creating new workbooks

    When a path is not fully qualified, Excel defaults to the directory that the module (the workbook the macro is save in) is located. This is why it will be different if the macro is ran from different files in different directories.

    Also, if the dir\folder\ path does not exist it won't create a folder.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  6. #6
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Error 1004 when creating new workbooks

    Quote Originally Posted by XeRo Solus View Post
    When a path is not fully qualified, Excel defaults to the directory that the module (the workbook the macro is save in) is located. This is why it will be different if the macro is ran from different files in different directories.

    Also, if the dir\folder\ path does not exist it won't create a folder.
    Thanks, XeRo, but I'm also getting different results from different files in the same directory.

    I'm also not trying to save into a non-existent folder; the new workbooks are saved into the same folder as the raw data file, the path for which is extracted from that file's name. I have verified that this path is correct, and all three workbooks are correctly saved to it despite the error. That's what's perplexing me - I'm getting a fatal error in a line of code that works anyway.

  7. #7
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Error 1004 when creating new workbooks

    I think I have located the source of the Error 9. Working from XeRo's comment I decided to find out what Excel thought the path was, so I added a new variable to the code:

    Please Login or Register  to view this content.
    Defining outputpath returns Error 9, so it seems there's something Excel doesn't like about the path. The path in question is "H:\Test junk\Recursive junk\".

  8. #8
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Error 1004 when creating new workbooks

    Apart from the fact you have some undefined variables (I don't recognize them as standard functions) I don't see a problem with your original code.

    Are the 3 new workbooks supposed to be saved in the same directory as the original fine containing the macro?

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    It's not the path Excel doesn't like it's fileStr.

    Try creating references to workbooks when you open or create them.

    For example:
    Please Login or Register  to view this content.
    If you do that you can the use the reference when you want to refer to the workbook you opened.

    Oh, and declare all your variables - adding Option Explicit at the top of the module will help with that.
    Last edited by Norie; 07-19-2013 at 08:50 AM.
    If posting code please use code tags, see here.

  10. #10
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Error 1004 when creating new workbooks

    Quote Originally Posted by XeRo Solus View Post
    Apart from the fact you have some undefined variables (I don't recognize them as standard functions) I don't see a problem with your original code.

    Are the 3 new workbooks supposed to be saved in the same directory as the original fine containing the macro?
    The three new workbooks are saved in the same directory as the data file. The simplified path structure would be G:\Macro\Data, with everything saved in the obvious directories.

    The code previously worked with the variables as-is. It would probably be worth filling the gaps in definitions just in case, but I'm not sure exactly where they are - some of the code I had to borrow from other sources.

    Norie - can you explain? The same error is produced even when I don't use fileStr to create the output filenames. What would you recommend I change?
    Last edited by Jedit; 07-19-2013 at 08:49 AM.

  11. #11
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Error 1004 when creating new workbooks

    Well the first undefined variables I notices are cellobject and NewBook. To find them just type option explicit at the top of the module before any procedures.

    What do you mean by obvious directories?
    Please Login or Register  to view this content.
    Will save it as the DRateFile variable as a filename but it will save in the host files directory because no path is defined.

    I created a dummy .csv on my desktop and ran the code from your OP and it created 3 new files on my desktop without any errors.

  12. #12
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Error 1004 when creating new workbooks

    Quote Originally Posted by XeRo Solus View Post
    What do you mean by obvious directories?
    I mean the macro is saved in the directory called Macro, and so on. The directories aren't actually called that, but that's the layout of the structure: macro interacting with data in a subdirectory, saving workbooks to that subdirectory.

    Please Login or Register  to view this content.
    Will save it as the DRateFile variable as a filename but it will save in the host files directory because no path is defined.

    I created a dummy .csv on my desktop and ran the code from your OP and it created 3 new files on my desktop without any errors.
    That's how it's meant to work, and how the original does work - three workbooks are created in the same directory as the data file.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Error 1004 when creating new workbooks

    In this line of code you have a variable 'ans' which is never given a value.

    What's it's purpose?
    Please Login or Register  to view this content.
    Also, why don't you put this code,
    Please Login or Register  to view this content.
    in here?
    Please Login or Register  to view this content.
    Something like this.
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Error 1004 when creating new workbooks

    Quote Originally Posted by Norie View Post
    In this line of code you have a variable 'ans' which is never given a value.

    What's it's purpose?
    Please Login or Register  to view this content.
    It's meant to be the position of the last backslash in fileStr. I have no idea why it uses a different variable, though; I wrote the original macro several years ago, and some of the code was borrowed. I've replaced it with fileStr.

    Good point about using the Workbook definition to set the top rows of the two main output workbooks. That's much tidier and is now implemented.

    Option Explicit has been added and all previously undefined variables are now defined. NewBook as Object is the only relevant one to the code in hand.

    Using the "Set wbFile" command does not fix the problem, but using it to open the source workbook triggers the thing where the debugger creates and saves the three workbooks before returning Error 9. Evidently the Error is occurring at the first use of the Set command, which suggests the issue may be in how I've set that up.

    Here's the current revision of the code:

    Please Login or Register  to view this content.
    Thanks everyone for your assistance so far, by the way.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Error 1004 when creating new workbooks

    Are you saying that if you use something like this you get a Subscript out of range error?
    Please Login or Register  to view this content.
    Also, using something like that triggers some other code?

    PS Minor thing, why have you declared NewBook and wbFile as Objects, their Workbook objects so use that.

  16. #16
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Error 1004 when creating new workbooks

    Quote Originally Posted by Norie View Post
    Are you saying that if you use something like this you get a Subscript out of range error?
    Please Login or Register  to view this content.
    Also, using something like that triggers some other code?
    Yes. Using that exact code returns Error 9 when the line is executed. Then the macro continues processing down to the saving of the third new workbook, even when F8ing in debug mode.

    I've seen a suggestion that in cases like this it's worth CTRL-F8ing for each line. Doing that, the Error 9 occurs when processing this line:

    Please Login or Register  to view this content.
    Using the wbFile definition I was able to replace that line by doing this:

    Please Login or Register  to view this content.
    This fixes the Error 9, but Error 1004 appears again in the next section of code:

    Please Login or Register  to view this content.
    CTRL-F8ing each line again seems to track it to a reference to Workbooks(SRateFile). It would make sense to get the same error again when I'm doing the same thing. I'll try replacing all references to other workbooks with a defined Workbook variable and see what happens.

    Edit: Error 1004 returned when attempting to execute the Do Until loop. The initial section is now fixed, though.

    (I'd still love to know why the debugger is processing multiple lines of code in stepwise mode.)
    Last edited by Jedit; 07-22-2013 at 05:06 AM.

  17. #17
    Registered User
    Join Date
    07-15-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Error 1004 when creating new workbooks

    Got it - it was the two-step solution to deleting the empty cells (select, then delete selection) that came from a recorded macro. Swapped it to a direct reference and it went away.

    The spreadsheet now processes completely without generating an error, so I'll close this thread off. Again, thanks everyone for your assistance.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Run time error 1004 when creating named range
    By csh8428 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-30-2012, 08:33 AM
  2. Creating Chart Programatically, error 1004
    By microcontrolled in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-20-2011, 05:26 PM
  3. Creating xls file via xml getting Run-time error '1004'
    By ydnam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-17-2006, 10:10 AM
  4. Unexpected error 1004 when using workbooks.open
    By AHD in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-06-2005, 07:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1