Looping through workbooks and counting non zero values

adjoa

New Member
Joined
Feb 27, 2017
Messages
2
I have this VBA code to count all values that are not zero in all excel files saved in a folder and print out the result in the workbook containing the macro. The problem I am having is that it opens the same file (the first one) over and over instead of moving to the next file to Count. Thanks for any help.

Sub RealCount()
Dim file As String
Dim row As Integer
Dim wb As Workbook
row = 2
file = Dir("\\Marknadskommunikation\FEB\*.xl??")
Do While file <> ""

Set wb = Workbooks.Open("\\Marknadskommunikation\FEB\*.xl??")
Call ZeroCount
file = Dir()

Loop

End Sub


Sub ZeroCount()

row = 2

ThisWorkbook.Sheets("Sheet1").Cells(row, 2) = Application.WorksheetFunction.CountIf(Range("B3:DE26"), "<>0")
row = row + 1

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Welcome to the board.

I think it's the wrong argument being passed to Set wb = Workbooks.Open, try:
Rich (BB code):
Sub RealCount()

    Dim file    As String
    Dim rng     As Range
    Const ROOT  As String = "\\Marknadskommunikation\FEB\*.xl??"
        
    Set rng = ThisWorkbook.Sheets("Sheet1").Cells(2, 2)
    
    file = Dir(ROOT)
    
    Application.ScreenUpdating = False
    
    Do While LenB(file) > 0
        rng.Value = ZeroCount(Workbooks.Open(file, ReadOnly:=True))
        file = Dir()
        ActiveWorkbook.Close False
        Set rng = rng.Offset(1)
    Loop
    
    With ThisWorkbook
           .Activate
           If ActiveSheet.Name <> "Sheet1" Then .Sheets("Sheet1").Select
    End With

    Application.ScreenUpdating = True
    
    Set rng = Nothing
    
End Sub

Private Function ZeroCount(ByRef wkb As Workbook) As Long


    Dim x       As Long
    Dim y       As Long
    Dim arr()   As Variant
        
    arr = ActiveSheet.Range("B3:DE26").Value
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        For y = LBound(arr, 2) To UBound(arr, 2)
            ZeroCount = --(arr(x, y) <> 0) + ZeroCount
        Next y
    Next x
    
    Erase arr
    
End Function
 
Last edited:
Upvote 0
Hello JackDanIce. Thank you for the alternative but unfortunately, it is still opening the same file (first one) and counting over and over. And that is after replacing the Word "file" with "ROOT" in rng.Value = ZeroCount(Workbooks.Open(ROOT, ReadOnly:=True)). Otherwise it gave an error that the file could not be found in the location...Oh and the Count came as negative too. Any other remedies?
Thanks in advance!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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