CounIf loop throught closed workbooks VBA value

AlbocIDO

New Member
Joined
Feb 16, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I'm trying to loop through closed workbooks in order to count the presence of a single person in a location monthly.
In the Countif it gives me type mismatching error, probably referring to the first range argument, that in this case is a string.
If it is the question, how can i arrange the procedure?

VBA Code:
Option Explicit

Public Function ContMonth(colleague As Range, location As Range, month As Range) As Long

Dim nameMonth As String
Dim nameLocation As String
Dim nameColleague As String
Dim rangeLocation As String
Dim stringMonth As String
Dim file As Variant
Dim count As Integer
        
nameMonth = month
nameLocation = location
nameColleague = colleague

Select Case True
    Case nameLocation = "ponte milvio"
        rangeLocation = "$A$2:$B$2"
    Case Else
        rangeLocation = "null"
End Select
        
Select Case True
    Case nameMonth = "January"
        stringMonth = "-01-2022"
    Case Else
        stringMonth = "null"
End Select
file = Dir("C:\Users\sbalo\Desktop\Test\*.xlsx")

While (file <> "")
    Do While InStr(file, stringMonth) > 0
        count = count + Application.CountIf("C:\Users\sbalo\Desktop\Test\" & "[" & file & "]" & "Sheet1" & "'" & "!" & rangeLocation, nameColleague)
    file = Dir()
    Loop
Wend
End Function

Sub Test()
Dim counter As Long
Dim name As Range
Dim location As Range
Dim month As Range
Set name = Range("A3")
Set location = Range("B2")
Set month = Range("B1")

    counter = ContMonth(name, location, month)
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Countif doesn't work on closed workbooks. You need to either open each workbook you are examining before you do the countif, or use some other method.
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,960
Latest member
AKSMITH

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