简体   繁体   中英

matching quarterly data to daily data excel

Given some data that looks like this:

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

The dates are misleading. Values for the month of January are actually quarterly, meaning I want to have every daily value from 1/31/1952 to 3/31/1952 to be 0.02.

More specifically, where the quarters are the months 1/2/3/4 in my data above:

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

Expected output:

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

How can this be done easily in excel?

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

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)

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

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM