+ Reply to Thread
Results 1 to 5 of 5

Getting Number of excel files inside different directories

Hybrid View

CntrlAltDel Getting Number of excel files... 10-27-2022, 03:35 AM
Marc L Re: Getting Number of excel... 10-27-2022, 04:33 AM
JEC. Re: Getting Number of excel... 10-27-2022, 04:41 AM
Marc L Try this ... 10-27-2022, 04:54 AM
CntrlAltDel Re: Getting Number of excel... 10-27-2022, 08:41 AM
  1. #1
    Registered User
    Join Date
    10-27-2022
    Location
    Lisbon
    MS-Off Ver
    Microsoft 365
    Posts
    2

    Question Getting Number of excel files inside different directories

    Hello

    My goal is to have a table where Column A is a independent entity, Column B is a number dependent on the number of files from a certain folder, Column C is the directory of the certain folder.

    ____
    For vizualization aid you can Imagine:
    Column A is a store (ex: Portuguese Store)
    Column C is a directory where every individual sale is stored as an excel document
    Columb B is the cell that will reveal the number of excel files (sales) inside the directory specified in column C

    Q:
    Trying to understand how I can add more stores and directories inside the same code
    ____


    I have a innitial draft for this but I can only get the number of files for one directory, if I try to reuse the variable to get the number of files for the following directories I find a bug.

    Draft:

    Sub CountFiles()
    Dim folder_path As String
    Dim strType As String
    Dim totalfiles As Variant

    folder_path = Worksheets("Files Count").Cells(2, 3).Value

    If Right(folder_path, 1) <> "" Then folder_path = folder_path & ""
    totalfiles = Dir(folder_path & strType)

    Dim i As Integer

    While (totalfiles <> "")
    i = i + 1
    totalfiles = Dir
    Wend
    Worksheets("Files Count").Cells(2, 2).Value = i

    End Sub


    How can I edit this code so that I can add as many directories and results as I need?
    Last edited by CntrlAltDel; 10-27-2022 at 04:00 AM.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Getting Number of excel files inside different directories


    Hello,

    is it under Windows only ?

  3. #3
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Getting Number of excel files inside different directories

    How about

    Sub jec()
     Dim ar, i As Long
     ar = Range("B2", Range("C" & Rows.Count).End(xlUp))
     
     With CreateObject("scripting.filesystemobject")
       For i = 1 To UBound(ar)
          ar(i, 1) = .getfolder(ar(i, 2)).Files.Count
       Next
     End With
     
     Range("B2", Range("C" & Rows.Count).End(xlUp)) = ar
    End Sub

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this ...


    A Windows only VBA demonstration for starters :

    PHP Code: 
    Sub Demo1()
        
    Dim WV(), R&
            
    = [A1].CurrentRegion.Columns(3)
            
    ReDim V(2 To UBound(W), 0)
        
    With CreateObject("Scripting.FileSystemObject")
            For 
    2 To UBound(W)
                If .
    FolderExists(W(R1)) Then V(R0) = .GetFolder(W(R1)).Files.Count
            Next
        End With
            Range
    ("B2:B" 1) = V
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  5. #5
    Registered User
    Join Date
    10-27-2022
    Location
    Lisbon
    MS-Off Ver
    Microsoft 365
    Posts
    2

    Re: Getting Number of excel files inside different directories

    Perfect!
    Thank you both, I've successfully solved the code thanks to your suggestions

+ 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. Embedding Picture Files as Pop ups inside Excel Files
    By cmf0106 in forum Excel General
    Replies: 13
    Last Post: 06-08-2021, 11:53 PM
  2. Excel VBA: Search folder and sub-directories for files matching partial string
    By terriertrip in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-16-2020, 03:27 PM
  3. Combine excel files named Final in all sub directories into one file
    By Vadimchik in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-28-2020, 01:00 PM
  4. VBA code required to count the number of files in multipe directories.
    By yaash in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2013, 05:43 AM
  5. Replies: 2
    Last Post: 03-09-2010, 09:54 AM
  6. [SOLVED] Renaming files from a number string inside the file.
    By Gordon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2006, 01:10 PM
  7. [SOLVED] How do I get excel files to open automatically from directories?
    By Damian in forum Excel General
    Replies: 2
    Last Post: 01-03-2006, 10:55 AM

Tags for this Thread

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