2017-04-15 66 views
0

我有這段代碼。當我設置rDate = Range("A14"),練成總是明白RDATE作爲值不是日期.if I set rDate as specific time (like 04/04/2017), iVal is 6.但是,如果我設置 RDATE =範圍(「A14」),IVAL = 0`(F欄包含日期和範圍(「A14」)= 04/04/2017)。我如何設置rDate作爲日期?Excel設置範圍有變量的日期

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 
+0

你的代碼(無效我的答案編輯後)應該工作。請告知它在做什麼,這不是預期的。 – YowE3K

+0

單元格'A14'是否包含日期?單元格F1:F..'包含日期嗎? 「Excel總是將rDate理解爲價值而非日期」是什麼意思? – YowE3K

+0

是的。 F1:F包含日期和範圍(「A14」)也包含日期。我想在列F中計算日期在單元格A14中包含日期 –

回答

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