Getting Number of excel files inside difference directories

CntrlAltDel

New Member
Joined
Oct 26, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
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 file 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?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi CntrlAltDel. Please use VBA tags around your code to make it easier to read. Press the little VBA icon in the toolbar of your post window and paste your code there.

VBA Code:
Option Explicit

Sub CountFiles()
    Dim folder_path As String, strType As String
    Dim lR As Long, lLast As Long, i As Long
    Dim vTotalFiles As Variant
    
    With Worksheets("Files Count")
        lLast = .Cells(.Rows.Count, 3).End(xlUp).Row
        
        For lR = 2 To lLast
            folder_path = .Cells(lR, 3).Value
            
            If Right(folder_path, 1) <> "\" Then folder_path = folder_path & "\"
            vTotalFiles = Dir(folder_path & strType)
            
            'Dim i As Integer  -  do your defining all at the top - easier for maintenance. If numbers can be big, use a Long instead of Integer
            
            While (vTotalFiles <> "")
                i = i + 1
                vTotalFiles = Dir
            Wend
            .Cells(lR, 2).Value = i
        Next lR
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top