将季度数据与日常数据相匹配

时间:2018-03-25 17:26:38

标签: excel

鉴于一些看起来像这样的数据:

date    CAY
1/31/1952   0.02
2/29/1952   0.03
3/31/1952   0.02
4/30/1952   0.03
1/31/1953   0.03
2/28/1953   0.03
3/31/1953   0.03
4/30/1953   0.02
1/31/1954   0.03
2/28/1954   0.03
3/31/1954   0.03
4/30/1954   0.03
1/31/1955   0.04
2/28/1955   0.03
3/31/1955   0.02
4/30/1955   0.02
1/31/1956   0.01
2/29/1956   0.00
3/31/1956   0.00
4/30/1956   0.00

日期具有误导性。 1月份的值实际上是季度值,这意​​味着我希望从1931年1月31日到1952年3月31日的每日值为0.02。

更具体地说,我的数据中的季度是1/2/3/4个月:

Quarter        Report period    
1              January 1 through March 31
2              April 1 through June 30
3              July 1 through September 30
4              October 1 through December 31

预期产出:

date        CAY
1/2/1952    0.02
1/3/1952    0.02
1/4/1952    0.02
1/7/1952    0.02
1/8/1952    0.02
1/9/1952    0.02
1/10/1952   0.02
1/11/1952   0.02
1/14/1952   0.02
1/15/1952   0.02
1/16/1952   0.02
1/17/1952   0.02
1/18/1952   0.02
1/21/1952   0.02
1/22/1952   0.02
1/23/1952   0.02
1/24/1952   0.02
1/25/1952   0.02
1/28/1952   0.02
1/29/1952   0.02
1/30/1952   0.02
1/31/1952   0.02
2/1/1952    0.02
...
4/1/1952    0.03
4/2/1952    0.03
...
7/1/1952    0.02
....
11/1/1952   0.03

如何在excel中轻松完成?

3 个答案:

答案 0 :(得分:1)

Run this through the data on Sheet1.

Option Explicit

Sub dailyData()
    Dim vals As Variant, d As Long, fd As Long, ld As Long

    With Worksheets("sheet1")
        fd = DateSerial(Year(Application.Min(.Columns(1))), 1, 1)
        ld = DateSerial(Year(Application.Max(.Columns(1))), 12, 31)
        ReDim vals(1 To (ld - fd + 2), 1 To 2)

        vals(1, 1) = "date": vals(1, 2) = "CAY"

        For d = fd To ld
            vals(d - fd + 2, 1) = d
            vals(d - fd + 2, 2) = .Cells(Application.Match(CLng(DateSerial(Year(d), Int((Month(d) - 1) / 3) + 2, 0)), .Columns("A"), 0), "B").Value
        Next d

        .Cells(1, "C").Resize(UBound(vals, 1), UBound(vals, 2)) = vals
        .Cells(1, "C").Resize(UBound(vals, 1), 1).NumberFormat = "mm/dd/yyyy"
    End With
End Sub

答案 1 :(得分:1)

To generate the dates, enter and select the first date, and Home tab -> Editing -> Fill -> Series... https://support.office.com/en-us/article/create-a-list-of-sequential-dates-aa1c0fa7-c76a-4762-8bc9-46f1691defec

Then next to the first date try this formula and fill down (adjust A2 to the cell of the first date, and quarterly!A:B to the source range) :

=LOOKUP( DATE(YEAR(A2), INT((MONTH(A2) - 1) / 3) + 2, 0), quarterly!A:B)

答案 2 :(得分:1)

Here's what I meant by helper columns - very basic but does work

enter image description here

In C2

=DATE(YEAR(A2),MONTH(A2)*3-2,1)

In D2

=EOMONTH(C2,2)

In E2

=D2-C2+1

In G2

=IF(G1="",1,IF(COUNTIF($G$1:G1,G1)<INDEX($A$2:$E$10,G1,5),G1,G1+1))

In H2

=IF(G1=G2,H1+1,INDEX($A$2:$E$10,G2,3))

In I2

=INDEX($A$2:$E$10,G2,2)

So Column G contains 91 1's 91 2's 92 3's etc. and is used to index back to the original data.