+ Reply to Thread
Results 1 to 5 of 5

Loop through sets of files and perform action on each set

  1. #1
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Loop through sets of files and perform action on each set

    I am trying to loop through a set of files which are in sets. With each set i want to update a value in the file to the same new value.

    Problem is in how to check when the first member of a new fileset is being read and to alter the value for the new set and then do the same for each new set encountered.

    The file sets are identifiable by the Left(filename,6) function. So when this changes it is a new set.

    So problem is how to read and store the first fileset identifier and then to loop, do the changes, detect the new filename and input the new value to that set.

    It looked easy but when storing the initial filename in the loop and comparing it to the next one it overwrites the stored variable.

    There must be an easy way round it but i am stuck!

    Thanks

    Neil

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: Loop through sets of files and perform action on each set

    Are all the sets in the same folder? If so, what is the full path to that folder? What is the extension of the files (xlsx, xlsm)? Are they the only files in that folder? In the workbook that will contain the macro, you will need a sheet that lists the left 6 characters of each set of filenames in one column and the value to be updated for each file set in the adjacent column. Assuming the value to be updated is in the same sheet in and in the same cell in each file, what is the name of that worksheet and what is the address of the cell to be updated?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: Loop through sets of files and perform action on each set

    Hi

    The files are all in the same folder with .tpl as extension. They are essentially text files with multiple lines with a parameter name and value on each line. They are the only files in the folder currently.

    I have code to loop through and read each file content (matching the extn .tpl) and split each line into a parameter name and value (stored in a dictionary so I can refer easily to any parameters' value).

    Currently, I can update a value ok and write it back.

    However, it is just the logic of altering a value or values in a SET of files with the same filename identifier that escapes me.

    So essentially I load the first file, it has an identifier of "AUDCAD" so update it with the value. The second file has again an identifier of "AUDCAD" so update that too and so on.

    At some point, the next file has a new identifier of "USDJPY" so for each of these the next value (which is looked up from another small array) is written and so on.

    There are currently no values stored in cells in the workbook just arrays handled by VBA. The code is run from excel so it does not rule that out if it helps, but it would be better handled in the VBA if possible.

    Not sure if that is clearer or not

    Regards

    Neil

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: Loop through sets of files and perform action on each set

    The only thing I can suggest is something that you may be able to incorporate into your existing code:

    Please Login or Register  to view this content.
    The code sets up an array of identifiers. It then loops through the array and checks the name of the first file before it is opened to see if the first 6 characters are a match to the values in the array. If there is a match, the file is opened, the value is updated and the file is closed. If there is no match, the loop goes to the next value in the array. In the place of "NameOfOpenFile" would be the variable used to loop through the files. I hope this makes sense.
    Last edited by Mumps1; 11-07-2018 at 11:43 AM.

  5. #5
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: Loop through sets of files and perform action on each set

    Hi i am getting "invalid qualifier"

    at line: - NameOfOpenFile.Close False

    Not sure what it means in this case as it is a string variable strFilename which will contain the filename, passed into the sub - sub test (strFilename)

    Ah i suspect it needs to be the objFile from the CreateObject("Scripting.FileSystemObject"). I will check.
    Last edited by webwyzard; 11-07-2018 at 05:50 PM. Reason: possible answer

+ 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. How to loop through filenames and perform an action based on filename criteria
    By webwyzard in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-31-2018, 07:51 PM
  2. [SOLVED] Loop down col A and find 2 criteria in a cell to perform an action on the data in between
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-20-2016, 08:32 PM
  3. [SOLVED] Loop through all worksheets in workbook to perform action fix code
    By AWITCHER in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-08-2016, 02:26 PM
  4. Perform same action on similar files and create one master list
    By Droriley in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-15-2015, 05:41 AM
  5. Loop through Autofilter Multiple Criterias and perform action
    By tahatkhan in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 03-22-2012, 04:07 PM
  6. If this = this, then perform this action
    By 00Able in forum Excel General
    Replies: 8
    Last Post: 11-21-2010, 03:52 PM
  7. get list of all files in folder, perform action on each
    By lowb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-14-2006, 05:30 PM

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