I have created a function which identifies the column of the country. Once identified it filters based on the string "W" and the month range. After which it is supposed to return number of rows remaining. The filtering occurs in another worksheet called "SUMMARY". But the function is called in a worksheet "INPUT". This works perfectly fine as a macro but not as a UDF. As a UDF, it doesnt filter but instead all the used rows in the "SUMMARY" worksheet. Let me know if there is a need for further clarification. Thanks!!
UDF Code
UDF Code
Code:
Public Function WorkingDays(StartDate As Date, Enddate As Date, Country As String) As Integer
SDate = CLng(StartDate)
EDate = CLng(Enddate)
Sheets("SUMMARY").Select
CtryColumn = Application.WorksheetFunction.Match(Country, _ Sheets("SUMMARY").Range("A4:Z4"), 0)
With Sheets("SUMMARY")
Selection.AutoFilter Field:=2, Criteria1:=">=" & SDate, Operator:=xlAnd _
, Criteria2:="<=" & EDate
Selection.AutoFilter Field:=CtryColumn, Criteria1:="W"
End With
For Each Rng In Sheets("SUMMARY").UsedRange.SpecialCells(xlVisible).Areas
x = x + Rng.Rows.Count
Next Rng
WorkingDays = x - 4
End Function