Consulting

Results 1 to 4 of 4

Thread: Loop through hundreds of files in a folder using select CASE

  1. #1
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location

    Loop through hundreds of files in a folder using select CASE

    I have different excel spreadsheets that will be in a directory i.e C:/Files/ folder. I will need to do different things with a lot of the files.

    e.g. All files beginning with "THER " will be of one kind. All files beginning with "APP" will be of another kind. There will about 20 other formats.

    Basically I need some code to loop through a specific folder that will pick out if the file begins with for example "THER" and then action some code. The code to action will depend on which file it has detected.

    CASE would be the best way and efficient way of managing the code I believe. Can anyone help?
    Last edited by satyen; 04-16-2008 at 03:51 PM.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Use the Dir function as here . The only change being
    [VBA]
    Arr = Array("THER", "APP")
    For Each a In Arr
    MyFile = Dir("C:\AAA\" & a & "*.*")
    Do Until MyFile = ""
    'do something with myfile
    MyFile = Dir
    Loop
    Next

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    If it Finds a file that begins with 'THER' it needs to action some code if it finds file that begins with 'APP' it needs to action some other code and so on for 200 other formats different code needs to be actioned. Will this work. Cannot I not use select CASE with a loop like this?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes something like this untested code

    [vba]

    Arr = Array("THER", "APP")
    For Each a In Arr
    MyFile = Dir("C:\AAA\" & a & "*.*")
    Do Until MyFile = ""
    Select Case a
    Case "THER": 'do something with myfile
    Case "APP": 'do something else with myfile
    End Select
    MyFile = Dir
    Loop
    Next
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •