2012-11-26 78 views
0

enter image description hereVBA間隔數據

我需要根據日期總結一些信息。我的意思是,如果與該信息對應的日期在一個區間內,我需要總結一些信息。

有沒有辦法做到這一點?

我所看到的「DATEDIFF」,但我需要什麼會是這樣的:

「如果evaluted的數據是whitin下一間隔總和值。

希望你能理解我的問題。

在此先感謝。

編輯:我添加了一張照片,使其更容易理解

+0

是否要在工作表中輸入某些值中的兩個日期?你知道如何做出改變嗎? :) – CustomX

+0

是的,我知道如何進行更改,再次感謝 – themolestones

回答

1

這將做到這一點。我不知道你想在哪裏使用總數,所以現在你只需要一個消息框。你想在和細胞

Sub SumBetweenTwoDates() 

Dim total As Integer 
total = 0 

Dim firstDate As Date, secondDate As Date 
firstDate = DateValue("20/11/2012") 
secondDate = DateValue("20/12/2012") 

For i = 1 To 5 
    If Range("A" & i).Value >= firstDate And Range("A" & i).Value <= secondDate Then 
     total = Range("B" & i).Value + total 
    End If 
Next i 

MsgBox total 

End Sub 
+0

,謝謝,我不知道我可以使用帶有日期的句子「IF」。 – themolestones

+0

你可以使用IF:P幾乎任何東西 – CustomX

1

使用一個公式:

假設值是A1:A100和日期是在B1:B100

=SUMPRODUCT((B1:B100>=DATEVALUE("1/1/2004"))*(B1:B100<=DATEVALUE("31/1/2004")),A1:A100) 

將返回總和2004年1月以後的值