+ Reply to Thread
Results 1 to 10 of 10

Count Worksheets in multiple files

  1. #1
    Registered User
    Join Date
    07-02-2015
    Location
    Minnesota
    MS-Off Ver
    Office 2010
    Posts
    5

    Count Worksheets in multiple files

    I need to build a macro that can read through a list of file names and return the number of worksheets that are present in each of the file (this is an audit mechanism to ensure that the correct number of worksheets are present in a series of files created via another process). The macro would need to establish the path of the folder where the files are located (all in the same folder), then locate the first file, identify the worksheet count, and repeat for the next file etc. For example the file requiring this functionality looks like:

    wkshtcnt.PNG

    I thought I could do this with a formula by simply referencing the file names but I believe Excel does not have a straightforward formula for worksheet counts.
    Thanks!

  2. #2
    Forum Contributor
    Join Date
    08-06-2012
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    119

    Re: Count Worksheets in multiple files

    Are the files going to be open when you run this code, and if not is it okay if the code opens them, counts the sheets, and then closes them?

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Count Worksheets in multiple files

    Hello dwb11,

    Do you want the path to put in the code or have the user select the folder?

    Are any of the workbooks password protected?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    07-02-2015
    Location
    Minnesota
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: Count Worksheets in multiple files

    The files would not be open. It would be ok if the code opens and closes them. I will add and additional piece of context in case this changes your thinking on this, the number of files will be large...over 2,000....
    Thank you for your help

  5. #5
    Registered User
    Join Date
    07-02-2015
    Location
    Minnesota
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: Count Worksheets in multiple files

    Preferably have the user select the folder (the user will probably be me so not overly concerned about "user friendliness" but upon each use of this the target files will be in a different folder).

    None of these files will be password protected.

    Thank you!

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Count Worksheets in multiple files

    Hello dwb11,

    This macro will look for the workbook names in column "A" starting in row 2 of "Sheet1". You can these settings to what you need. You will also need t ochange the workbook file path to where your files are located.

    Macro Code
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-02-2015
    Location
    Minnesota
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: Count Worksheets in multiple files

    Thank you Leith Ross!
    I made the necessary adjustments to orient your code to my file and this works beautifully! One thing I noticed is that the returned worksheet count is always 1 more than actually exists in the file (is it possible that the tab that appears as a default in excel thatyou click to add a new tab being counted?) I manually added worksheets to check this and it is always +1. I can work around this but thought I would ask if you can think of an adjustment for this. Just so you know, the only change I made to your code (other than the location references) was to remove the 'n=n+1' statement in the ' Copy the count to the cell one column to right of the workbook name in the list' section - the original code recorded the results in every other row as opposed to consecutive rows. I only point this out in case this has anything to do with the +1 worksheet count issue that I mention.
    Thanks for your help.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Count Worksheets in multiple files

    Hello dwb11,

    Sorry about that, that was my fault. Here is updated and tested macro code.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    07-02-2015
    Location
    Minnesota
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: Count Worksheets in multiple files

    Hi Leith Ross, this is perfect! Thank you very much for your expertise and help!

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Count Worksheets in multiple files

    Hello dwb11,

    You're welcome.

+ 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. Deleting Multiple Worksheets from Multiple Excel Files
    By monica1991 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 06-01-2015, 02:35 PM
  2. Replies: 2
    Last Post: 06-17-2014, 11:16 AM
  3. count unique values in multiple worksheets with multiple criteria
    By craig04 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-06-2012, 03:26 PM
  4. Replies: 1
    Last Post: 12-05-2010, 04:39 AM
  5. Importing multiple text files into multiple worksheets
    By new.vbacoder in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 10-24-2010, 08:34 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