Excel设置范围具有日期作为变体

时间:2017-04-15 03:50:08

标签: excel-vba date variant vba excel

我有这个代码。当我设置rDate = Range("A14"), excel时,请始终将rDate理解为值而不是日期.示例if I set rDate as specific time (like 04/04/2017), iVal is 6.但是如果我设置 rDate = Range(“A14”),则ival = 0`(F列包含日期和范围(“A14”)= 04/04/2017)。如何将rDate设置为日期?

Dim rDate As Range
Set rDate = Range("A14")
iLastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
iVal = Application.WorksheetFunction.CountIf(Range("F1:F" & iLastrow), rDate)
Sheets(1).Activate
Range("C14").Value = iVal

1 个答案:

答案 0 :(得分:1)

尝试下面的代码(代码注释中的解释):

Option Explicit

Sub CountIfDates()

Dim rDateRng    As Range
Dim rDate       As Double
Dim iLastrow    As Long
Dim iVal        As Long

With Worksheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
    Set rDateRng = .Range("A14")
    rDate = rDateRng.Value ' <-- get the date value and store the the numeric value

    iLastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    iVal = Application.WorksheetFunction.CountIf(.Range("F1:F" & iLastrow), rDate)
End With

Sheets(1).Range("C14").Value = iVal

End Sub