Efficiently Batch Read Hundreds of Files

I need to read hundreds of Excel files to count and retrieve data only from of highlighted cells. I’ve tried the code below, but I encounter an OLE error when processing more than 60 files. Is there a better approach I could use, such as PowerShell, SQL, Python or another application?

The VBA CODE I used

Sub CountHighlightedCellsInFiles()
    Dim objExcel As Object
    Dim listFilePath As String
    Dim outputFilePath As String
    Dim fileNames As Variant
    Dim results As String
    Dim Workbook As Workbook
    Dim Worksheet As Worksheet
    Dim cell As Range
    Dim baseFileName As String
    Dim colorIndex As Long
    Dim val As Variant
    Dim fileNameParts As Variant
    Dim fileResults As String
    Dim sheetFound As Boolean
    
    ' Create Excel Application object
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = False  ' Run in the background
    objExcel.DisplayAlerts = False  ' Disable alerts
    
    ' Define the paths
    listFilePath = "C:UsersDocumentslist.txt"
    outputFilePath = "C:UsersDocumentsoutput.txt"
    
    ' Read file names from the text file
    fileNames = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(listFilePath).ReadAll, vbCrLf)
    
    ' Loop through each file name
    For Each Filename In fileNames
        On Error Resume Next  ' Start error handling
        Set Workbook = objExcel.Workbooks.Open(Filename)
        
        If Err.Number <> 0 Then  ' Check if an error occurred
            results = Filename & vbTab & "#N/A (Error Opening File)" & vbCrLf
            Err.Clear  ' Clear the error
            GoTo WriteResults  ' Skip to writing results
        End If
        
        ' Initialize temporary results for the current file
        fileResults = ""
        sheetFound = False
        
        ' Loop through each worksheet to find the one matching the pattern
        For Each Worksheet In Workbook.Worksheets
            If Worksheet.Name Like "Sheet1" Then  ' Adjust pattern as needed
                sheetFound = True
                
                ' Loop through each cell in the specified range
                For Each cell In Worksheet.Range("B3:AK60")
                    ' Check if the cell's interior color is not automatic (highlighted)
                    colorIndex = cell.Interior.colorIndex
                    If colorIndex <> -4142 Then  ' -4142 represents xlNone
                        ' Get the value of the highlighted cell
                        val = cell.Value
                        
                        ' Append the value to the file results string
                        If fileResults = "" Then
                            fileResults = val
                        Else
                            fileResults = fileResults & vbTab & val  ' Use vbTab for separation
                        End If
                    End If
                Next cell
                
                ' Exit the loop after processing the first matching sheet
                Exit For
            End If
        Next Worksheet
        
        ' Get the base file name without extension
        fileNameParts = Split(Filename, "")
        baseFileName = Left(fileNameParts(UBound(fileNameParts)), Len(fileNameParts(UBound(fileNameParts))) - 5)
        
        ' Combine the base file name with the accumulated values or #N/A
        If sheetFound Then
            If fileResults <> "" Then
                results = baseFileName & vbTab & fileResults & vbCrLf  ' Use vbTab for separation
            Else
                results = baseFileName & vbTab & "#N/A" & vbCrLf  ' Use #N/A when no highlighted cells
            End If
        Else
            results = baseFileName & vbTab & "#N/A (Sheet Not Found)" & vbCrLf
        End If
        
WriteResults:
        ' Write the results to the output text file after each file is processed
        Open outputFilePath For Append As #1
        Print #1, results
        Close #1
        
        ' Close the workbook without saving changes
        If Not Workbook Is Nothing Then
            Workbook.Close False
            Set Workbook = Nothing  ' Release the workbook object
        End If
        
        ' Allow Excel to process other events
        DoEvents
        
        ' Introduce a slight delay to reduce OLE errors
        Application.Wait Now + TimeValue("00:00:01")  ' Wait for 1 second
    Next Filename
    
    ' Quit the Excel application
    objExcel.Quit
    
    ' Release objects
    Set Worksheet = Nothing
    Set objExcel = Nothing
End Sub

I have a link to a zip file that includes ‘list.txt’ and ‘output.txt’ files, as well as a couple of Excel files for demonstration purposes to illustrate the issues I have encountered.
Hope this clears up the issues I’ve been facing.

New contributor

yui_2000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật