1

I want to know the number of times a value is repeated, for example 288, and how many values there are in total (every number) in many files with the same format.

For one worksheet I would just use =COUNTIF(F:F;288) and =COUNTA(F:F)

But now I have to do it with more than 30000 xlsx files inside a folder.

My first intent was to merge them into one file like this and then count with this solution, but it stopped after 5279 tabs, I guess for some kind of limitation.

All my files are in the same folder (H:\Macro\positions) and the values are only expected in column F.

There are between 100-600 values per file, around 30000 files.

The operation has to be done just once, I don't mind waiting some hours for it to finish.

How would you do it?

4
  • 1
    Are all the xlsx in the same folder, and are the values expected only in column F in each sheet in each file in that folder? Commented Jun 16, 2017 at 8:48
  • Yes, H:\Macro\positions and just in column F. Commented Jun 16, 2017 at 8:50
  • How many values have to be counted? I mean 30000 Files are alot. opening excel files takes a lot of time, thats why you should merge them in some way. you could cycle trough the merged files then and store the counter in an array. Commented Jun 16, 2017 at 8:56
  • There are between 100-600 values per file, around 30000 files. The operation has to be done just once, I don't mind waiting some hours for it to finish. Commented Jun 16, 2017 at 9:01

1 Answer 1

2

Try the code below and follow the comments - basically the code opens each spreadsheet in the given folder, loops through the sheets in that workbook, runs your COUNTIF formula for each sheet and keeps a record of the total count.

Option Explicit Sub CheckForValue() Dim objFso As FileSystemObject '<-- add Microsoft Scripting Runtime as a reference Dim objFile As File Dim wbToCheck As Workbook Dim wsToCheck As Worksheet Dim strPath As String Dim varValue As Variant Dim lngValueCount As Long Dim lngTotal As Long Dim wsf As WorksheetFunction On Error Goto CleanUp strPath = "H:\Macro\positions" Set objFso = New FileSystemObject '<-- access to file system varValue = 288 '<-- value you are looking for lngTotal = 0 '<-- total count of value you are looking for Set wsf = Application.WorksheetFunction '<-- shortcut to WorksheetFunction ' iterate files in folder For Each objFile In objFso.GetFolder(strPath).Files ' only check spreadsheets If objFile.Type = "Microsoft Excel Worksheet" Then ' get reference to workbook Set wbToCheck = Workbooks.Open(objFile.Path) ' iterate worksheets For Each wsToCheck In wbToCheck.Worksheets ' your original formula lngValueCount = wsf.CountIf(wsToCheck.Range("F:F"), varValue) ' add to total lngTotal = lngTotal + lngValueCount Next wsToCheck ' close without saving changes wbToCheck.Close SaveChanges:=False End If Next objFile ' final count of value you are looking for Debug.Print "Total is: " & lngTotal CleanUp: ' error handling If Err.Number <> 0 Then Debug.Print Err.Description End If Set objFile = Nothing Set objFso = Nothing End Sub 

Based on your comment that The operation has to be done just once, I don't mind waiting some hours for it to finish then the above code will do that, just grinding through sheets checking for the value. If you want to improve the speed you can use the following code before the For loop to help:

Application.ScreenUpdating = False Application.DisplayAlerts = False Application.EnableEvents = False 

And then afterward turn the settings back (after the CleanUp: statement):

Application.ScreenUpdating = True Application.DisplayAlerts = True Application.EnableEvents = True 
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it and worked for a small sample. I will now use it with my 30000 files and let you know if it scaled well. Thank you very much.
Reading the edit now, perfect, I will use the speed up.
I corrected the edit - the code to turn the settings back on should be in the CleanUp block in case there is an error. I also added an On Error Goto CleanUp command.
It has finished now! Perfect. I will modify the code to count other values, the total number etc, but now I know how to do it. Thank you very much.
No problem - glad it helped.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.