+ Reply to Thread
Results 1 to 3 of 3

loop through all files in a folder except for one of them

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-20-2012
    Location
    stockholm
    MS-Off Ver
    Excel 2010
    Posts
    123

    loop through all files in a folder except for one of them

    Hi,

    I'm having a macro that loops through all files in a specific folder.

    part of the code is

    strMyFile = Dir(strMyDirectory & "*.xls*")

    Do Until strMyFile = ""
    Set wrkTargetWrkBook = Workbooks.Open(strMyDirectory & strMyFile)
    If InStr(wrkTargetWrkBook.Name, " ") > 0 Then
    Application.Run ("'" & wrkTargetWrkBook.Name & "'!" & strMyMacro)
    Else
    Application.Run (wrkTargetWrkBook.Name & "!" & strMyMacro)
    End If
    Application.EnableEvents = False 'do not run macro at events such as closing
    ActiveWorkbook.Close True
    strMyFile = Dir
    Loop

    could I make the loop skip one of the files (master.xls) so that the action are not performed in that file?

    Thanks,
    Martin

  2. #2
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: loop through all files in a folder except for one of them

    Add another condition to your code.
    If strMyFile = "master.xls" then 
    'don't do anything
    else
    'insert your code here
    end if

  3. #3
    Forum Contributor
    Join Date
    11-20-2012
    Location
    stockholm
    MS-Off Ver
    Excel 2010
    Posts
    123

    Re: loop through all files in a folder except for one of them

    Hi PingPing,
    Thanks for your reply - I'll try this. Just to be sure - beteen 'then' and 'else' in your code I shall not enter any code at all?

    All the best,
    Martin

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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