+ Reply to Thread
Results 1 to 13 of 13

VBA macro only works once.

Hybrid View

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

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

+ 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