+ Reply to Thread
Results 1 to 21 of 21

Run time error 1004 - why is the last line of this code causing this?

  1. #1
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Run time error 1004 - why is the last line of this code causing this?

    The below macro is something I am trying to write to get a file to extract and tab into a master template, and then save the template in a specific file location and name. For some reason it falls over with run time error 1004 at the very last line "ActiveWorkbook.SaveAs spath & sfilename"

    Does anyone know why?!

    Thank you!



    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Last edited by alansidman; 03-04-2014 at 08:40 AM. Reason: code tags

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,893

    Re: Run time error 1004 - why is the last line of this code causing this?

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between
    Please Login or Register  to view this content.
    tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

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

    Re: Run time error 1004 - why is the last line of this code causing this?

    Bad idea giving a sub the same name, SaveAs, as a VBA method.
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    276

    Re: Run time error 1004 - why is the last line of this code causing this?

    what Norie said.

    In order to troubleshoot, try putting

    Please Login or Register  to view this content.
    in just before the activeworkbook.saveas command, just so you can check that a valid file path is being generated.

  5. #5
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    Thank you all,

    I have added in the msgbox and it looks like the right file path, but then still comes up with the run time error.

    It creates the file with the correct file name, but just stops short of actually saving it.

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

    Re: Run time error 1004 - why is the last line of this code causing this?

    Try changing the name of the sub so it's not the same as the VBA method SaveAs.

  7. #7
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    Sorry about that, rules read and understood, an oversight on my part, brain drain caused by macro poblems!!

  8. #8
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    Hello All,

    I have tried the suggestions above, and tweeked the code to be more specific about the file type (so it is macro enabled), but I get the same error message, run time error 1004 and the last line of code is highlighted.

    I'm wondering if the problem is because I am copying the whole sheet with a copy / paste function rather than righting code to duplicate the tab, but then I'm not sure how to do this so will record myself doing it and see what the code looks like!

    Thank you for your help with this, I'm quickly running out of time to get this working, eek!




    Please Login or Register  to view this content.

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run time error 1004 - why is the last line of this code causing this?

    what are the actual values of spath and sfilename?
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  10. #10
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    Hi,

    The values of spath is fixed up until the last folder in the directory, which is named after a cell in the file, which would be something like A563403 (which is cell C4) and then cell B4 is a name, for example Bletchley. So I am telling the macro to save the file in the folder F:\Landfill............\A563403 Bletchley.

    The sfilename is then also based off of cells in the file and would be the A563403 and then Scenario2 (from cell B6).

    The theory is then that the user can select a cost centre, select a scenario, run the macro which then saves a file is the relevant location. It is always getting stuck at the last line and giving me the same message.

    I hope that all makes sense!

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run time error 1004 - why is the last line of this code causing this?

    what happens if you change the code to
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    Thank you for that!

    I ran it and the error message, path not found comes up, so there must be something wrong with my file path. I shall go over it with a fine tooth comb!

  13. #13
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Run time error 1004 - why is the last line of this code causing this?

    sPath has backslash on end so I think you must use:
    Please Login or Register  to view this content.
    to test for directory.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  14. #14
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    I have tried that as well now, it now reverts back to the old error message of run time error 1004. The part it highlights is the ActiveWorkbook.saveas

    Please Login or Register  to view this content.

  15. #15
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Run time error 1004 - why is the last line of this code causing this?

    May you save this manually without error using same file name and path? No invalid characters in real data? No permission problems?

  16. #16
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    I can save it manually without any problems. I have just recored myself doing it manually, and this is the code I get.

    I can sucessfully change the code so that the file path is linked to the cells, but when I do the same with the file name it falls over. I will make the changes again and post what I end up with when it crashes.

    Here is the 'clean' code froom my recording:

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    Firstly, thank you to everyone for your suggestions, I feel I am making progress!

    Here is the code (from above) after make the File path link to two cell references. The macro still works at this point. When I try and do the same to the file name coding it falls over. So my question is, how do I adjust the file name coding so that the last part of the folder directory is the same as the file path, and the file name links to cell C4 and cell B6 of the INPUT sheet?

    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    I have recorded myself saving the file manually and have managed to change the coding so that the file path links to the relevant cells in the file.

    At this point the macro works perfectly, but when I try and change the file name coding to work in the same way, it falls over and highlights the entire file name coding. What coding should I write so that the last part of the file directory in the file name coding replicates the file path coding, and then what coding should I use to tell the macro that the file name comes from cell C4 and B6 of the INPUT sheet?

    Here is the coding that currently works:

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    Sorry folks, I posted the samething twice above, my pc crashed and I didn't think it posted, so I did it again, only to discover it had posted my response!

  20. #20
    Registered User
    Join Date
    02-06-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Run time error 1004 - why is the last line of this code causing this?

    This is now solved, here is what I ended up doing:

    Please Login or Register  to view this content.
    Thank you to everyone for your help!

  21. #21
    Registered User
    Join Date
    07-11-2014
    Location
    Coimbatore, India
    MS-Off Ver
    2010
    Posts
    5

    Re: Run time error 1004 - why is the last line of this code causing this?

    Try using this instead:

    Please Login or Register  to view this content.
    Where xlApp is your Excel Application.

    It worked for me in a similar problem

+ 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. Replies: 4
    Last Post: 11-15-2013, 05:03 PM
  2. VBA Code...error = run time error 1004 autofilter method of range class failed
    By Dariusd7 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-15-2013, 04:49 PM
  3. VisibleItemsList is causing error: 1004
    By kkode in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-01-2012, 05:44 PM
  4. Run Time Error 1004 on code
    By aftabn10 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-24-2011, 09:46 AM
  5. Run time Error 1004 in 5th line of IF..THEN..ELSE
    By go-fish in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-19-2010, 05:17 AM

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