Results 1 to 13 of 13

VBA macro only works once.

Threaded 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

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