Extend the Macro to run for all files in a file-folder

ragav_in

Board Regular
Joined
Feb 13, 2006
Messages
89
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Dear members,

Currently I am working on collating the number of records from multiple workbooks in a given folder and paste the "workbook name" and "count" in adjacent cells (see below table). What I want to perform is loop though all the files in a given folder, open each workbook (with only one sheet), execute the below code and then get the workbook name and count of records and populate them in another workbook or worksheet (from where the macro is run).

The macro I have shown below currently applies AutoFilter on a certain Column named "Loaded" and filter for records that have "N" as values and count the total number of records with values N. My requirement is to extend this macro to run on a specific folder and perform this macro and populate the workbook name and number of rows in another worksheet.

VBA Code:
Sub RecNotLoaded()
Dim i As Integer, rngData As Range, notLoaded As Integer, mysheet As String
Set rngData = Range("A1").CurrentRegion

'To check if the Autofilter is On or Off and Toggle
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
ActiveSheet.Range("A1").AutoFilter

'Perform the autofilter on Column titled "Loaded" and Criteria is "N"
i = Application.WorksheetFunction.Match("Loaded", Range("A1:AZ1"), 0)
rngData.AutoFilter Field:=i, Criteria1:="=N"

'Count the number of rows filtered with this criteria
notLoaded = rngData.Columns(1).SpecialCells(xlCellTypeVisible).count - 1

'get the Worbook Name
mysheet = ActiveWorkbook.Name

'This below code can get the worksheet name and number of rows in pastes in A3 and B3 respectively.
'Sheets("Sheet5").Select
'Range("A3").Value = mysheet
'Range("B3").Value = notLoaded

End Sub

Expectation or Output from the Code is as below
Workbook NameNotLoaded
US_Workbook1.xlsx72
EU_Workbook5.xlsx27
Dubai_Workbook6.xlsx190
China_Workbook7.xlsx500

It would be of great help if you can help me to extend this code to perform my task. Now doing it manually is time consuming and sometimes has errors. Would want to avoid such manual errors and also reduce the time required to perform this task which is a daily task with multiple files across the globe.

I thank each and everyone for their time and effort to read though this and provide me with a solution.

Thanks in advance,
ragav_in
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Adjust in the macro, the name of your sheet to put the results and the name of the folder where you have the files.

Try this:
VBA Code:
Sub count_n()
  Dim sh As Worksheet, sh1 As Worksheet
  Dim sPath As String
  Dim sFile As Variant
  Dim wb1 As Workbook
  Dim f As Range
  Dim n As Long, i As Long
  
  Application.ScreenUpdating = False
  
  Set sh = Sheets("Summary")  'Set the name of your sheet
  sPath = "C:\trabajo\files\" 'Set the name of your folder
  
  sh.Rows("2:" & Rows.Count).ClearContents
  sFile = Dir(sPath & "*.xlsx")
  i = 2
  Do While sFile <> ""
    Set wb1 = Workbooks.Open(sPath & sFile)
    Set sh1 = wb1.Sheets(1)
    If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
    Set f = sh1.Range("1:1").Find("Loaded", , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      n = WorksheetFunction.CountIf(sh1.Columns(f.Column), "N")
      sh.Range("A" & i).Value = sFile
      sh.Range("B" & i).Value = n
      i = i + 1
    End If
    wb1.Close False
    sFile = Dir()
  Loop

  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,687
Messages
6,126,204
Members
449,298
Latest member
Jest

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