Flatten Monthly data series

Source: Internet
Author: User

In recent data processing, the rainfall data collected in one place is arranged by month, as shown in the following table:

Station Year Type Month 1 2 3 4 ... 29 30 31
bj0030c 1961 Precip 01 0 0 0 0 ... 0 0 0
bj0030c 1962 Precip 01 0 0 0 0 ... 0 0 0
bj0030c 1963 Precip 01 0 0 0 0 ... 0 0 0
bj0030c 1964 Precip 01 0 0 0 0 ... 0 0 0
bj0030c 1965 Precip 01 0 0 0 0 ... 0 0 0
bj0030c 1966 Precip 01 0 0 0 0 ... 0 0 0
bj0030c 1967 Precip 01 0 0 0 0 ... 0 0 0
bj0030c 1968 Precip 01 0 0 0 0 ... 0 0 0
bj0030c 1969 Precip 01 0 0 0 0 ... 0 0 0
bj0030c 1970 Precip 01 0 0 0 0 ... 0 0 0

In order to get a daily data sequence, the following macro code is written:

 Public Subcombinedates ()DimWssrc asWorksheet, WsResult asWorksheetDimS1 as String, S2 as String    DimI as Integer    DimInvalidsheet as Boolean        SetWSSRC =ActiveSheet'Check Source FormatInvalidsheet =False    IfWssrc.cells (1,1). Text <>" Station"  ThenInvalidsheet =True    IfWssrc.cells (1,2). Text <>" Year"  ThenInvalidsheet =True    IfWssrc.cells (1,3). Text <>"Type"  ThenInvalidsheet =True    IfWssrc.cells (1,4). Text <>"Month"  ThenInvalidsheet =True     fori =1  to  to            IfWssrc.cells (1,4+ i). Text <> I ThenInvalidsheet =True    Next    IfInvalidsheet Then        MsgBox "Invalid source Sheet."& VbCrLf &"The first row of the sheet must be:"& VbCrLf & _            "Eg gh id,year,eg el abbreviation,month,1...31", vbcriticalExit Sub    End If    'Create The result sheetS1 = wssrc.name &"_RLT"     on Error Resume NextS2=S1 i=1     Do        SetWsResult = Nothing        SetWsResult =activeworkbook.sheets (S2)IfWsResult is  Nothing  Then Exit  DoS2= S1 &"("& I &")"I= i +1    Loop     on Error GoTo 0    SetWsResult =ActiveWorkbook.Sheets.Add (, wssrc) Wsresult.name=S2'ConvertWsresult.cells (1,1). Value =" Station"Wsresult.cells (1,2). Value ="Date"Wsresult.cells (1,3). Value =wssrc.name Wsresult.columns (2). ColumnWidth = A    DimRowidx as Long, ROWIDXRLT as Long, Curyear as Integer, Curmonth as IntegerRowidx=2ROWIDXRLT=2     while  notIsEmpty (Wssrc.cells (ROWIDX,1)) S1= Wssrc.cells (Rowidx,1). Text Curyear= Wssrc.cells (Rowidx,2). Value Curmonth= Wssrc.cells (Rowidx,4). Value fori =1  to  to            IfIsEmpty (Wssrc.cells (rowidx, i +4)) Then Exit  forwsresult.cells (ROWIDXRLT,1). Value =S1 wsresult.cells (ROWIDXRLT,2). Value =DateSerial(Curyear, Curmonth, i) wsresult.cells (ROWIDXRLT,3). Value = Wssrc.cells (rowidx, i +4). Value ROWIDXRLT= Rowidxrlt +1        NextRowidx= Rowidx +1WendMsgBox " in total"& (ROWIDXRLT-2) &"records were generated.", vbinformation,"Congratulation"End Sub

Flatten Monthly data series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.