+ Reply to Thread
Results 1 to 43 of 43

Loop macro for all Excel files in folder

  1. #1
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Loop macro for all Excel files in folder

    Hey Gang,

    I'm pretty new to VBA and was looking here for someone with more experience. I need to create a macro that I can loop to apply changes to multiple files in the same folder. Not sure if this is possible, but I thought I'd ask.

    Below is a summary of the changes I need to apply to each file

    1) Clear the contents of any cell that contains a currency format
    2) Change the year of any date that from 2016 or 2017 to 2017 and 2018 respectively
    3) Clear the contents of the cell directly to the right of a cell including the text "Approximate # of persons covered at end of policy or contract year"

    Notes:
    Only the year of each cell with a date needs to be changed to one year ahead
    The cell containing the text in quotes above may contain other text in it as well, (will not be a complete match)
    The position of the cells that need to be changed will not appear in the same place on every worksheet

    Any guidance would be much appreciated and I welcome any questions you may have. I have attached what I was able to create so far...

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: Loop macro for all Excel files in folder

    you can do it but you have to open each file individually

    https://stackoverflow.com/questions/...es-in-a-folder

    So open a file make your changes If they work on an individual file, close the file saving changes and loop through all files in a folder

    it also depends on the filetype of the files you need to open are they all xls? or csv? etc

  3. #3
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    thank you for your timely response! All the files have .xlsx extensions. There's about 2000 of them. However, the code for clearing the cell to the right of the cell including the text "Approximate # of persons covered at end of policy or contract year" will not work if there is other content in that cell. Sometimes there are other letters, symbols, or dotted leaders but that text string is always the same. Any ideas on how to overcome that hurdle would be much appreciated.
    Last edited by akenney151; 02-05-2018 at 01:38 PM.

  4. #4
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Loop macro for all Excel files in folder

    Possibly...
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    Alright, it looks like that did the trick! Thank you so much, now I just have to figure out how I'm going to loop this for every excel file in my folder, and that will be a lot of time saved. You're a life saver!!

  6. #6
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Couple questions:
    • Does each file only have a single sheet named Sheet1? If there are multiple sheets per file do you need to check every sheet?
    • Are the workbooks you need to check all in the same parent folder? If so are there any subfolders we need to look in too?
    • You mention the things you are looking for are not always in the same sheet column, but do they always have the same header? (IE: might the dates column have the word "Date" always on x row above the dates)

    Couple recommendations:
    • I would do this in such a way that you have an output folder and copy each file, make your changes, save it to output folder (so that you have originals in case something doesnt work or you need to check anything later).
    • I would provide a warning dialog prior to running to let the user know this may take a long time and allow them to either continue or cancel (as Excel will be tid up until the macro completes).

    Couple things I notice with the code as is (and it would change drastically if you need to apply it to many files, as you will be looping and wont be hardcoding things like Activeworkbook).

    rng is set to A1:Z1000. Does each sheet have content in that range? Is there ever more content or is that beyond the max expected? If your sheets never have that much content then you are much better off going with UsedRange. This way you ensure you always get/evaluate all the used cells on a sheet and nothing more (which can waste time).

    As mentioned you can use InStr() to check for a phrase.

    Looping cells is very slow. Especially when you do it 2 times instead of a single time. You may be able to skip the loop in some places or reduce the number of cells you need to loop (IE: unless dates on a single sheet are spread out willy nilly you may be able to find the first, then only loop down the rows in that column for example).

    EDIT: can you post a sample file that we can check out and try things on to help you?
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Loop macro for all Excel files in folder

    This is untested - your code is between the commented asterisks - it saves changes, so take care:

    Please Login or Register  to view this content.
    Last edited by xladept; 02-05-2018 at 03:51 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  8. #8
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    To answer your questions

    Each file has 3 sheets but there is only content on sheet 1, that is the only sheet that needs to be edited
    Every workbook is in the same folder
    As for the dates, each one has a label one cell to the left, most of them are in columns b and c but I haven't checked all 2000 to know if they will always be there.
    there's only 2 dates usually to change in the whole sheet anyway

    Every sheet has content in the range A1:Z1000, to be honest though I meant to change that to A1:I150 because the range was not nearly that big, these excel sheets are pretty small and there wouldn't be any content outside that range.
    **Not sure what a used range is**

    The files have sensitive information on them, so unfortunately I won't be able to help you out there. Hope this answers a few questions for you.

  9. #9
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    Would I need to add anything to you code xladept, to get the macro to run. I just tried testing it on a couple of files on my desktop and I didn't see any changes.

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Loop macro for all Excel files in folder

    It runs on my system if I Change Cell to c in your code i.e.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    Ok I made your adjustment and it seems to work on a test folder, it doesn't affect the open workbook I used to run it on, but the other file was changed when I opened it. I'm going to do some more tests and report back anything strange. Thank you so much for all your h

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Loop macro for all Excel files in folder

    You're welcome and thanks for the rep!

    BTW - Running "Option Explicit" is something you should do

  13. #13
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    The macro doesn't like in when it tries to clear merged cells. Some of the cells formatted with currency are merged. Is there a quick fix for that?

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Loop macro for all Excel files in folder

    Merged Cells can be a problem lest they're all merged the same way - we can unmerge them??

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  15. #15
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Quote Originally Posted by akenney151 View Post
    The files have sensitive information on them, so unfortunately I won't be able to help you out there.
    You could still make some sample files without the sensitive info in it? Primarily so we have an idea of the structure of your data, not the data itself. Things like where you have headers, patterns in the file we can use for the code, etc.

    As you mentioned, with only ~2 date values its a bit silly to loop every single cell looking for dates. You may instead consider using the range.find method or something like it, hard to say without a sample.

    UsedRange is the range of cells Excel considers to be "in use" on a given sheet. In other words the usedrange should end at the last row and column you have actual content in on the sheet. You even use it once in your own code:

    Please Login or Register  to view this content.
    Used range is likely better than using a static range like A1:Z1000 as for each file/sheet used range will be exactly the range of cells and no more. So if a sheet really only has content on A1:Z100 you are checking 2600 cells more than you need to using a static range. If another sheet has content A1:ZZ20000 then you are missing most of the data using a static range.

    To manually check the used range on a sheet select A1 and on the keyboard do Ctrl+Shift+End. What is selected is the used range on that sheet (so you have an idea of what used range means).

    Tabular information tends to have headers in the column with each row being a record in the data set (presuming the data is tabular). Is there a row of headers in these files? Can we use those headers to find columns like a date column?

    @xladept code will get you closer in that it will loop files, but it doesn't address some of the issues I mentioned in my prior post. For example you loop all the cells in the range using a For each loop and then do it a 2nd time instead of handling actions on the cells all in one For loop. Thats double the work for each file x2000 files.

  16. #16
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    Hey guys, here are some attachments to let you know what I'm working with and what changes I want to make.
    In addition I also found that I have to change a year that is not written as a date from 2016 to 2017 (see sheet for details)
    Hope this clears things up a bit. I will continue trying out your suggestions and follow up with updates. Thank you all for your support!!
    Attached Files Attached Files

  17. #17
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Ok so a couple observations I can make now that I have seen some samples.

    Your data is arranged like a form, with labels to the left and data to the right (I know you explained it as such, but seeing it shows us this doesnt vary).

    Knowing you have labels, can we make any assumptions about what the labels will be across the 2000 files? IE: will there always be a label towards the top of the sheet that says "For Year:"? Will the dates you want to change always have the same labels?

    Lets assume your sheet(s) have 100 used cells in column A and B. Looping each cell just to find 5-6 values only makes sense when those 5-6 values can be in any of those cells. If, however, we can find those values according to a unique entry/label then there are ways to find just those things very quickly by comparison.

    For example, I wouldnt loop all your cells to find "For Year:" presuming that label is the same in all of your files. Instead I would use range.find which will return the range object of that cell (ex: A2). I could then simply offset 1 column to the right of it and set the .value of the range as desired (2017).

    So the big question is, can we look for specific labels to make changes or are they not consistent enough (a couple variations on the labels is ok, 100 different variations on a single label...too much to be feasible)?

    If you are not sure, spot check your files. Open a couple from more recent, a couple a bit older, a couple a bit older than that, etc. See if you notice any change in patterns like vastly different labels or layout.

    This is also why I recommend either making copies of them and using the copies or having the macro leave the originals and make copies...so that going back you can account for any missing labels or any labels you didnt want to change but that got changed due to an unexpected match to your conditions.

    Let me know if you think using the labels is a viable approach to finding the content you want to change.

    Also is this 1 and done (ie: change the 2000x files and never do this again) or is this something you will need to repeat again at some point? Where do these files come from? May be worth considering standardizing the files (maybe be possible to automate some of the changes, especially the dates).
    Last edited by Zer0Cool; 02-06-2018 at 10:34 AM.

  18. #18
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    Assumptions:

    - For the most part there will always be a label somewhere near the top that says "For Year"
    - For the most part there will always be a label somewhere for "Plan year beginning" and "Plan year ending" dates
    - For the most part the values I need to change or alter will always be to the right of it's corresponding label
    - Dollar figures could be anywhere in the form in varying numbers but will always be for the most part in currency format.
    - For the most part there will always be the text "Approximate # of persons covered at end of policy or contract year" somewhere in the form

    While the cells I need to clear or alter will always be found to the right of the labels, the labels will not always be found in the same place on each form. In addition, the cell I want to clear or change will sometimes be merged with the cell next to it.

    There is also a possibility of spelling mistakes or incomplete forms, but it would be rare. If one of the forms does have an abnormality, I would want to flag it somehow and for the macro to continue running for the remaining files in the folder.

    From what I understand these files were created by my company and they are somewhat standardized, but not enough to make this easy. If I had any control over that I would, but I don't so I'll keep my mouth shut.

  19. #19
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Loop macro for all Excel files in folder

    Still, I would need a sample worksheet with all idiosyncrasies.

    Anyway, thanks for the rep!

  20. #20
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    There should be 4 sample files on the 1st page of this thread, unless you want a different sample. @xladept, your code seems to work but it will not clear or alter merged cells. The cells cannot be unmerged, but for the most part they all seem to be merged the same way: 1 cell deep and 2 cells wide across columns B and C.

  21. #21
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Loop macro for all Excel files in folder

    Possibly...
    Please Login or Register  to view this content.

  22. #22
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Based on your samples and description in post #18 it seems like:
    • You have at least 2 variations on the layout
    • Much of what you need to find can be identified by a label
    • Once the label is found, the value to change should be the cell to the right of it
    • The only change I see that cant be found via label is the dollar amounts.

    So with that in mind, I think we would need to:
    1. Identify how many "types" of layouts there are, have the macro identify which a given file is to apply the proper logic to it
    2. Either have the macro make copies of the modified files to a specific directory or assume we are working from copies
    3. Come up with a list of specific labels and if they are unique to a specific layout or universal across all the files to check.
    4. As you have mentioned, somehow log exceptions. Maybe generate a txt or Excel file listing information like ex: File name/path, label(s) missed.

  23. #23
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    I have tried running your code @dangelor and it seems that it ran through for every sheet and it output to the right folder, but all the dollar amounts and the for year date were still the same. The only changes that applied were to the plan year beginning and plan year ending dates and clearing the "approximate # persons..." cell. Is there anything else I should try?

    - @ZerOcool,

    I think your observations are correct. The labels I mentioned before - For Year, Plan Year Beginning/Ending, and Approximate # .... - should be universal across all forms. Although they won't always be in the same place, they will always be in the usedrange and the cell I need to alter or delete will always be 1 to the right of those exact labels - so no matter what layout the form is in there will always be those key indicators. The dollar amounts as you figured could be anywhere and in varying numbers within the usedrange. The challenge will be with the merged cells - (you can see how they appear in the test sheets)

  24. #24
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Loop macro for all Excel files in folder

    Moved the currency check into the for/next loop.
    Please Login or Register  to view this content.

  25. #25
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Quote Originally Posted by akenney151 View Post
    The labels I mentioned before - For Year, Plan Year Beginning/Ending, and Approximate # .... - should be universal across all forms.
    In the 4 files you posted however, "For Year" is not in #1 samples for example. Is that not a correct representation of your actual files then? It is safe to work on the notion that they all share those labels? If so should we base or testing on the Test 2 Before sample?

    I am not too worried about the merged cells. Also the position of the labels isnt a big deal, as long as the label itself is consistently the same text (at least partially) then finding them without looping all cells is easy.

    Ill see what I can draft up. Unless I find a really easy way to log the exceptions, I may leave that part up to you.

    EDIT: just noticed the Test 2 Before file has rows 23-24 a label as just "label" with a date to the right. So to clarify what labels do we need to find to adjust dates (excluding the From year label)?
    • Plan Year Beginning
    • Plan Year Ending

    Are there any other labels representative of a date in the file? Can a label be repeated multiple times in the same file and if so do we act on all of them then?
    Last edited by Zer0Cool; 02-07-2018 at 03:08 PM.

  26. #26
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    @dangelor - We're getting closer, all the cells in number format were cleared, Plan Year Beginning & Ending dates were changed, but The For year date stayed the same, and when I went to the output folder, only one document made it. Not sure if it still loops for every file in the folder. Let me know if there's something else I can try or explain for you. Thanks for all your support!

  27. #27
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    Most of the files will have a For Year label and a corresponding date, but if there isn't a For Year date in the Test 1 Before and After then there won't be a For Year date to worry about. The labels will consistently include the same text - sometimes there will be dotted leaders or other letters/symbols in front and the labels will only appear once in every workbook on sheet 1.

    The dates in rows 23 & 24 like you mentioned are just formulas to display the same dates as the Plan Year Beginning and Plan Year Ending dates. Other than that there won't be any other dates in the forms. Hopefully that explains it. Thanks again for all your help!

  28. #28
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    I noticed some of the cells with currency format have formulas in them, in that case do you still want to delete the contents of the cell (formula included), clear the formatting or ignore those cells?

    I have by the way already written the code to handle everything but the currency formats, including clearing merged cells. I made 4 copies of your before 2 file and adjusted values in each one. Macro takes a split second to complete. Ill post it when complete.

    EDIT: Also, do you want to clear only the contents of the currency cells or remove the currency format as well?
    Last edited by Zer0Cool; 02-07-2018 at 05:04 PM.

  29. #29
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Loop macro for all Excel files in folder

    This seems to work:

    Please Login or Register  to view this content.

  30. #30
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    See attached. I have zipped the file with the macro in it and a folder of the before and after results.

    You will need to change 2 constants in the macro prior to using it. The constant for source folder and for destination folder. These are at the top of the module and all code is clearly commented.

    The macro loops the files in a source folder, modifies them as follows:
    • Find the label for your dates, change the cell to the right +1 year
    • Find the label for "For Year" and increase the cell to the right by 1
    • Find the long "Aprox..." string and clear the cell to the right.
    • Find all cells formatted as currency and clear contents (currently regardless of static/formula and currently doesnt clear currency formatting from cell)

    It saves them to an output folder specified in constants.

    I even managed to slip some logging into my error checker, but its very basic and just outputs to the immediate window.

    I made some assumptions in the macro:
    • All the files you want to modify are in the same source folder with no other files in the folder
    • You will have created a folder to place the resulting modified files into prior to running the macro
    • You have updated the constants for source and destination folder to match your setup
    • The source files all follow the same pattern we discussed and hopefully I understood it correctly
    • Both speed and readability are important. (important for it to work quick but also important for you to be able to follow why it works)

    Test it out, let me know if you have questions.
    Attached Files Attached Files

  31. #31
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    @Zer0cool - for the cells with currency formats that have formulas, it's ok to clear all contents formula and format included.

  32. #32
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Ok cool, it currently clears the formula and contents, not the formatting (ie if you typed a number back in the cell itd be currency).

    Test the file i posted and if you like it we can tweak it as needed.

  33. #33
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Loop macro for all Excel files in folder

    I added a check for "For Year". As for the file loop, it opens only files that have the value of sExt in the file name.
    Please Login or Register  to view this content.

  34. #34
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    Ok so I have some feedback for all you guys. I've tried all three macros and here's the results.

    @xladept - the macro you had posted at reply 29, successfully loops through all files in the folder applying changes to all files and saves them in the same folder, but takes a little longer to complete. Also I noticed the For Year date was cleared and replaced with a 1 in the cell right above it. In addition, one of the other forms has numbers separted by comma, which the macro changed into a date. - (my bad didn't mention that before). Other than that the numbers formatted in currency were cleared, the Plan Year Beginning/Ending dates were updated, and the "approximate # persons..." cell was cleared.

    @dangelor - your most recent macro also successfully loops through all the files in the folder and places the updated workbooks in the output folder. It runs pretty fast, and all the changes were made correctly, but the numbers separated by commas were changed to dates - (again my bad didn't mention that before)

    @Zer0cool - your macro was huge and intimidating with all the comments but it successfully loops through all the files in the folder and places the updated workbooks in the output folder. It also runs pretty fast, and all the changes were made correctly. Somehow it didn't even care that there were some numbers separated by commas and just left those alone. So.... it works perfectly .

    Thank you to all of you for all your help, you've been incredibly supportive! If there's anything else you would recommend I do I'm all ears, and god bless VBA.

  35. #35
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Quote Originally Posted by akenney151 View Post
    @Zer0cool - your macro was huge and intimidating with all the comments but it successfully loops through all the files in the folder and places the updated workbooks in the output folder. It also runs pretty fast, and all the changes were made correctly. Somehow it didn't even care that there were some numbers separated by commas and just left those alone. So.... it works perfectly .
    Glad to hear its working. The macro is longer than it seems because I over commented it so you could follow the logic, but in the hopes that the way I wrote the macro and comments would be efficient AND easy for a person to read. I also tend to be very explicit in my declarations and constants, which adds some heft to it initially but makes it easier to update and scale.

    For example, lets say you have an additional label to check for in the future, lets say the label is "OMG Zer0Cool is Awesome" (haha). You would not have to change the structure of my macro much. You add that string to the constant of strings comma delimited and expand the if statement according to the action to take. The loop itself should work with it, no need to worry about the method not working if you need to expand on it. The same is true for currency formats. Lets say you find across the 2000 files that 3 different currency formats have been used, then you just add the additional ones to the formats constant, pipe "|" delimited and your done, all 3 types of currency format are cleared. Easy.

    Also in testing I would imagine you are using a small subset of files and not close to 2,000. I have a feeling my macro should be as fast as can possibly be. Lets say its .5 seconds faster per file than other methods provided, across 2000 files that should save you roughly 16 minutes of time (unless im too tired for math, 1000 seconds = ~16 mins?).

    My approach does not loop cells, which is generally slow. Instead I am using the built in Find and Replace functions of Excel which should be significantly faster, especially in larger ranges. In my macro I am actually looping the terms/labels you want to find and the currency format(s) you want to find which should make it easier to update the macro for new terms/formats should you need to.

    Lastly, I also added error handling. I do this in every macro I write as a user should never be presented with a debug option. They should also get a clear message as to why something didn't work. As part of my error handler I have it log the file name, label that couldn't be found and error into the immediate window (could be expanded to dump into Excel or a text file).

    If you have any questions about the code let me know.
    Last edited by Zer0Cool; 02-08-2018 at 10:57 AM.

  36. #36
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Loop macro for all Excel files in folder

    Glad you have a solution that works for you!

  37. #37
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Loop macro for all Excel files in folder

    I'm also glad that you're satisfied - and, thanks for the rep!

    I noticed the For Year date was cleared and replaced with a 1 in the cell right above it.
    That means that the starting row is one below the sample.
    Last edited by xladept; 02-08-2018 at 12:24 PM.

  38. #38
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    Hi guys!

    Thanks for all your help on that project. One other thing to mention is that all the sheets were protected to begin with so I wrote a simple macro to unprotect everything in the folder. Was just curious if there was a faster way to do it. Below is what I was able to come up with.

    Please Login or Register  to view this content.

  39. #39
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Do you intend to keep the Sheet Protection, IE unlock the sheet, make changes, relock it or do you want to remove sheet protection so the results are no longer protected?

    Also from your snippet I am presuming you:
    1. Know the password
    2. The password is the same for all sheets

    Is that correct?

    If you want to keep the protection, you know the password and its the same for all sheets then your method is the best way to handle it.

    Essentially get the file, open it, unprotect sheet, do stuff, reprotect, close file, next. It could be incorporated into my File looping routine in the macro I provided pretty easily.

    However, if you do not want to keep protection and/or if the password varies or you may not know the password then the best method is to simply strip out the Sheetprotection via XML (presuming all the files are OpenXML format, xlsx, xlsm).

    The reason for this is starting in Excel 2013 (afaik) MS increased the complexity of the hashing algorithm used to protect and unprotect sheets (link here). In manual use there is no noticeable impact on the end user but when combined in a macro across 100's or 1,000's of sheets it can add a very measurable difference in the time it takes to do. Despite this, they still do nothing to protect the underlying XML, so I found there is no such penalty for simply stripping out the XML in the file structure that tells it the sheets protected. This is a more complex approach to doing it and may be overkill for what you need anyhow. In addition, I am not comfortable sharing my code to do this in case it was used for malicious purposes.

    I think the change to the stronger hashing adds about ~1/10th of a second per hash vs the pre 2013 hashing if I recall. Nominal for 1 or 2 sheets, seconds or minutes more for thousands of sheets.
    Last edited by Zer0Cool; 02-08-2018 at 02:09 PM.

  40. #40
    Registered User
    Join Date
    02-05-2018
    Location
    NH
    MS-Off Ver
    2016
    Posts
    19

    Re: Loop macro for all Excel files in folder

    I do know the password for all the sheets and it stays the same for every workbook. I don't have to keep sheet protection, we had it before but it was only meant for specific cells and it was done improperly, so we are just removing it altogether.

  41. #41
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Ok so then what you have is basically what you want. Your code removed the protection but did not re-add it. Actually benefits you as its 1 hit vs 2 for each sheet. If you like ill update the macro I gave you and attach it

  42. #42
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Loop macro for all Excel files in folder

    Updated file, now with 100% more sheet unprotecting!

    Basically added constant at top for the password (change as needed) and an if statement in the GetFiles routine. It checks if the sheet is protected, if so it attempts unprotecting with the password constant.

    I have not tested the code myself, so certainly test it to ensure it works. I also have not added any error handler to specifically address if any sheet cannot be unlocked.

    Also, an alternative method to this would be to use:

    Please Login or Register  to view this content.
    This essentially lets VBA mess with the sheet without unlocking but keeps the sheet locked for the user. Only beneficial if you want to keep protection.
    Attached Files Attached Files

  43. #43
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Loop macro for all Excel files in folder

    This should run more quickly:

    Please Login or Register  to view this content.

+ 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. [SOLVED] Loop through multiple files, run macro, then save as Excel workbook in a different folder
    By Peter Kallio in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 08-26-2013, 11:42 PM
  2. [SOLVED] Macro to loop through all files in a folder
    By aldo23 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-01-2013, 10:21 AM
  3. [SOLVED] Loop Through all files in a folder and use the same macro to edit each one.
    By dj59 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-18-2012, 06:56 PM
  4. Macro to loop through all files in a folder troubleshooting (excel for mac)
    By TSMIII in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-31-2011, 09:31 AM
  5. Loop to open excel files in a folder
    By elmarko123 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-02-2010, 12:58 PM
  6. Macro to Loop Through Full Folder of TXT files
    By dvent in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-13-2009, 12:24 PM
  7. Macro - Collect data - Select folder and loop through all xls files
    By zmalach in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2008, 10:37 AM
  8. Macro - Collect data - Select folder and loop through all xls files
    By kostas in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-04-2008, 05:58 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