+ Reply to Thread
Results 1 to 13 of 13

VBA macro only works once.

  1. #1
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Question VBA macro only works once.

    Hey guys,

    So I have code written to merge the data from multiple spreadsheets into one. It works fine when I first write the program and press play, I can even press play multiple times and it will append the data to the bottom of the spreadsheet. However, if I save it and close it (with or without hitting play) and try to run the program once I open it I get an error.
    VBA Error.jpg
    Is there something I did wrong or am missing? Any help would be most appreciated.

    Here is a copy of the code that I used.
    'Finding files in folder

    Please Login or Register  to view this content.
    Last edited by alansidman; 10-03-2013 at 02:59 PM. Reason: added code tags

  2. #2
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: VBA macro only works once.

    hi.
    When you get an error message it is most helpful to state which line the error occurs on (it will be highlighted in yellow).

    The next thing to do is to follow the code through and see if it is doing what you expect. To do this:
    1. click on the first line of the sub and press F8 then carry on pressing F8 and you will see which line is being executed.

    My Guess is that find it goes wrong on the workbooks open line. So the question then will be : what value is in myfile. so insert this in the line before: debug.print now, myfile this will display the changing value of myfile in the VBA immediate window.


    click on the * Add Reputation if this was useful or entertaining.

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,702

    Re: VBA macro only works once.

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

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] 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 here



    (I have added the code tags for you since you are new to the forum. Please adhere to our rules 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

  4. #4
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: VBA macro only works once.

    Thanks for your help.
    You are correct about the workbooks open line.
    So for the first run it shows all the files within the folder.
    Please Login or Register  to view this content.
    Whenever I re-open it it only gives me the first one.
    Please Login or Register  to view this content.
    Here is the code after debugging.
    Please Login or Register  to view this content.
    I have the code saved on a non-macro excel spreadsheet and whenever the old one fails I save delete it and make a new spreadsheet with all the code, enable macros, and then run it and it works fine.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA macro only works once.

    Maybe ...

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: VBA macro only works once.

    The code you provided works, but then excel crashes! :/
    When it re-opens I tried it again and it gave me the same workbooks.open error.

    I apologize, I am just REALLY new at this, literally started yesterday.

    I don't understand why it can't find the file if it's in the same place.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA macro only works once.

    Try this:

    Please Login or Register  to view this content.
    Try stepping through and find out what line causes the crash.

  8. #8
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: VBA macro only works once.

    So again it works the first time but crashes.

    It seems that after it finds all the files and extracts from them it starts to attempt to do it to the zmaster file, which obviously causes problems.

    Upon Re-opening I added a line I don't know is valid but worth a try
    Please Login or Register  to view this content.
    I went through the code one line at a time and it went down fine until I reached workbooks.open and then it gave the classic suppliers-a.xlsx cannot be found.

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA macro only works once.

    Try shg's code with your:
    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  10. #10
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: VBA macro only works once.

    Well I am glad you are progressing. Isuggest changing the workbooks open line to
    Please Login or Register  to view this content.
    and add aline just before the other debugof
    Please Login or Register  to view this content.
    I suspect that might show the problem.
    Last edited by tony h; 10-03-2013 at 09:11 PM.

  11. #11
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: VBA macro only works once.

    I added that to my original code and the other code and they both worked great.

    Thank you!!!!

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA macro only works once.

    You're welcome!

  13. #13
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: VBA macro only works once.

    That's great. For the sake of completeness it would be nice to see the final code.

    Also I presume you have realised that you don't need to leave the debug lines in the code. They only display values to help you understand what is going on.

+ 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: 5
    Last Post: 05-04-2013, 01:42 PM
  2. [SOLVED] Macro error when ran on multiple sheets on workbook (even though macro works)
    By steven_e in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2012, 06:09 PM
  3. can record macro, but RTE5 when ran. Macro works fine on another PC.
    By boyd98 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-27-2011, 01:10 PM
  4. [SOLVED] Excel Addin works that works on a template workbook
    By s.jay_k in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-15-2006, 03:35 PM
  5. [SOLVED] How do I convert works file to excel without works software?
    By CatMB in forum Excel General
    Replies: 1
    Last Post: 06-21-2005, 12:05 PM

Tags for this Thread

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