+ Reply to Thread
Results 1 to 5 of 5

Loop through multiple files and call macros (but unable to loop)

  1. #1
    Registered User
    Join Date
    02-27-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    20

    Loop through multiple files and call macros (but unable to loop)

    Hi,

    I require some assistance with a macro which loops through a number of files and calls the same macro in each of them. Unfortunately when I add "Application.Run..." to the code, it no longer loops through the process and instead stops after updating the first file in the loop. If I remove the "Application.Run..." code and add any other code, the loop works fine and it continues through the process repeating all the steps for each file found.

    I was wondering if anyone might know why it stops after one file when using "Application.Run..." to call the macros?

    NB I have a list of path and file names starting in row 8 of columns A and C. Each file in the list has a macro called UpdateS1 and promoupdate1.



    Sub C_Run_Loop_Macro()
    Dim lastRow As Long
    Dim i As Long
    Dim WB As Workbook
    thiswkb = ActiveWorkbook.Name
    Range("D5").ClearContents
    Range("D8:D29").ClearContents

    On Error Resume Next
    For i = 8 To Range("B" & Rows.Count).End(xlUp).Row
    Set WB = Workbooks.Open(Cells(i, "A").Value & "\" & Cells(i, "B").Value)
    If Not WB Is Nothing Then

    '-------------------------------------------------------------------------------
    nwkb = ActiveWorkbook.Name
    Application.Run ("'" & nwkb & "'!UpdateS1")
    Application.Run ("'" & nwkb & "'!promoupdate1")

    -------------------------------------------------------------------------------

    Else
    MsgBox "Please check path & folder for row " & i, vbExclamation
    Exit Sub
    End If
    Next i
    nwkb = Nothing
    Set WB = Nothing
    Cells(5, 4).Value = "Completed"
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Loop through multiple files and call macros (but unable to loop)

    your code seems correct, attach please sample files
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    02-27-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Loop through multiple files and call macros (but unable to loop)

    I'm just returning to this post - I've been off work.
    Unfortunately it's difficult to attach files because they are 45mb in size.
    Is someone able to run a macro which loops through a list of files while using the code "Application.Run...". This appears to happen to several macros that I've created and all of them do not loop when the Application.Run is included in the code?

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Loop through multiple files and call macros (but unable to loop)

    sample files, not original files but small files with the same structure

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Loop through multiple files and call macros (but unable to loop)

    Hi, ryanpetersen,

    before any more action in this thread should be taken you should try to comply with the Forum Rules and mind to add code-tags around the procedures posted here. As the oiginal post is older than 2 days you would need to ask a moderator to update as you may only edit the post for 2 days.

    Is it the same macro in every workbook? If so why donīt you run it from the workbook that you open the other workbooks? Do you mind sharing the codes with us? The best tip we may offer is disabling the line
    Please Login or Register  to view this content.
    and at least single step through the code by placing the mouse inside the pocedure and pressing F8 in order to find out what happens or if any error is raised.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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. VBA Loop for a folder of all the files, Loop all the worksheet in each workbook
    By nanjingwoodworking in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-07-2013, 07:20 PM
  2. [SOLVED] Repeat macros in a loop and save as word files
    By amanduggal in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-07-2012, 08:39 AM
  3. Call Macros before Next loop (loop creates new worksheets)
    By mantooth29 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2012, 05:47 PM
  4. Unable to Loop Multiple Worksheets
    By jackc in forum Excel General
    Replies: 4
    Last Post: 01-05-2006, 08:50 AM
  5. [SOLVED] Loop Function unable to loop
    By Junior728 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2005, 06:05 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