I have a user form that has 2 calender controls, the user selects the beginning and ending dates then clicks the execute button. The execute button is suppose to count the number of rows that match the date range the user selected and return it a label control. Here is my code, which by the way is all wrong and does not work, in it I have commented out ideas that I tried but would only match the dates exactly not the range of dates.
Private Sub CmdBtn_Click()
'Perform search of column 'D' and return
'results to LblNum
Dim num As Integer
Dim rng As Range
Dim date1 As Date, date2 As Date
'ActiveSheet.Unprotect
date1 = CalForm.Calendar1.Value
date2 = CalForm1.Calendar2.Value
Range("D:D").AutoFilter Field:=1, Criteria1:=">=" & date1, Operator:=xlAnd, Criteria2:="<" & date2
num = .Cells.Count
'If date1 = date2 Then
' num = Application.CountIf(Columns("D:D"), date1)
'Else
' num = Application.CountIf(Columns("D:D"), date1) + Application.CountIf(Columns("D:D"), date2)
'End If
LblNum = num
'ActiveSheet.Protect
End Sub
Bookmarks