+ Reply to Thread
Results 1 to 13 of 13

VBA macro only works once.

Hybrid View

  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

    Sub LoopThroughDirectory()
        Dim MyFile As String
        Dim erow
        MyFile = Dir("C:\Basic Data Transfer\")
    'Making sure file has data
        Do While Len(MyFile) > 0
    'Ignoring File in folder
        If MyFile = "zmaster.xlsm" Then
        Exit Sub
        End If
    'Open the files in the folder
        Workbooks.Open (MyFile)
    'To copy specific cells from files
        Range("A2:A3:D2:D3").Copy
    'Close the files
        ActiveWorkbook.Close
    'Finding first empty row in master spreadsheet
        erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    'Paste the data onto active sheet, on blank cells just determined
        ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1), Cells(erow, 4))
    'Repeating for the next file
        MyFile = Dir
        Loop
        
    End Sub
    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
    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.
    10/3/2013 6:27:01 PM        supplier-a.xlsx
    10/3/2013 6:27:01 PM        supplier-b.xlsx
    10/3/2013 6:27:01 PM        supplier-c.xlsx
    Whenever I re-open it it only gives me the first one.
    10/3/2013 6:31:32 PM        supplier-a.xlsx
    Here is the code after debugging.
    'Finding files in folder
    Sub LoopThroughDirectory()
        Dim MyFile As String
        Dim erow
        MyFile = Dir("C:\Basic Data Transfer\")
    'Making sure file has data
        Do While Len(MyFile) > 0
    'Ignoring File in folder
        If MyFile = "zmaster.xlsm" Then
        Exit Sub
        End If
    'Open the files in the folder
        Debug.Print Now, MyFile
        ->Workbooks.Open (MyFile)
    'To copy specific cells from files
        Range("A2:A3:D2:D3").Copy
    'Close the files
        ActiveWorkbook.Close
    'Finding first empty row in master spreadsheet
        erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    'Paste the data onto active sheet, on blank cells just determined
        ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1), Cells(erow, 4))
    'Repeating for the next file
        MyFile = Dir
        Loop
        
    End Sub
    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.

  4. #4
    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,700

    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] [/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

  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 ...

    Sub LoopThroughDirectory()
        Dim sFile       As String
    
        sFile = Dir("C:\Basic Data Transfer\*.*")
    
        Do While Len(sFile)
            If sFile <> "zmaster.xlsm" Then
                Debug.Print Now, sFile
                Workbooks.Open sFile
                Range("A2:D3").Copy Sheet1.Cells(Rows.Count, "A").End(xlUp)(2)
                ActiveWorkbook.Close SaveChanges:=False
                sFile = Dir
            End If
        Loop
    End Sub
    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:

    Sub LoopThroughDirectory()
        Dim sFile       As String
    
        sFile = Dir("C:\Basic Data Transfer\*.xl*")
    
        Do While Len(sFile)
            Debug.Print Now, sFile
            If LCase(sFile) <> LCase(ThisWorkbook.Name) Then
                Workbooks.Open sFile
                Range("A2:D3").Copy Sheet1.Cells(Rows.Count, "A").End(xlUp)(2)
                ActiveWorkbook.Close SaveChanges:=False
                sFile = Dir
            End If
        Loop
    End Sub
    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
    Do While Len(sFile)
            Debug.Print Now, sFile
            If LCase(sFile) <> LCase(ThisWorkbook.Name) Then
                Workbooks.Open (sFile)
                Range("A2:D3").Copy Sheet1.Cells(Rows.Count, "A").End(xlUp)(2)
                ActiveWorkbook.Close SaveChanges:=False
                sFile = Dir
            End If
        Loop
        Application.Stop (Now + TimeValue("0:00:05"))
    End Sub
    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:
    Dir("C:\Basic Data Transfer\")
    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
                Workbooks.Open "C:\Basic Data Transfer\"  &  sFile
    and add aline just before the other debugof
    debug.print curdir$
    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