+ Reply to Thread
Results 1 to 8 of 8

VB Code to convert Quarter data into Monthly data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-31-2012
    Location
    Georgia
    MS-Off Ver
    Excel 2010
    Posts
    194

    VB Code to convert Quarter data into Monthly data

    Hello:

    Please refer to attached file.
    I have Quarterly Gross Earning report for employees as shown.
    Data shows 2nd Qtr ending (6/30/2020) and 3rd Qtr Ending (9/30/2020).

    I need VB Code to split this data into Monthly instead of Quarterly by
    dividing 3 each earning in column D as example

    Data shows 2nd row,
    6/30/2020 Alyssa Mccoon $2990.31
    So split this data as
    4/30/2020 Alyssa Mccoon $976.77
    5/31/2020 Alyssa Mccoon $976.77
    6/30/2020 Alyssa Mccoon $976.77

    Manually completed data is shown in Sheet2

    Please let me know if you have any questions.
    Thanks.

    R
    Attached Files Attached Files

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: VB Code to convert Quarter data into Monthly data

    A power query solution

    let
        Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"TotInc", Currency.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [TotInc] / 3),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", Currency.Type}}),
        #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Custom.1", each Date.AddMonths([Date],-1)),
        #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.2", each Date.AddMonths([Date],-2)),
        #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Added Custom2", {{"Date", type text}, {"Custom.1", type text}, {"Custom.2", type text}}, "en-US"),{"Date", "Custom.1", "Custom.2"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"Merged"),
        #"Removed Columns" = Table.RemoveColumns(#"Merged Columns",{"TotInc"}),
        #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Removed Columns", {{"Merged", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Merged")
    in
        #"Split Column by Delimiter"
    Attached Files Attached Files
    Last edited by alansidman; 11-12-2022 at 06:33 PM.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: VB Code to convert Quarter data into Monthly data


    Quote Originally Posted by rmomin View Post
    Manually completed data is shown in Sheet2
    Hello,

    so the reason why so many dates errors or your initial explanation does not match your expected result ?!

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,086

    Re: VB Code to convert Quarter data into Monthly data

    See next code in Sheet1 code module

    Option Explicit
    
    Sub Prepa()
    Const DstCol = 7
    Dim LR As Long, I As Long
    Dim Totinc As Single, MonInc As Single
    Dim EmpN As String
    Dim QDate As Date, M1Date As Date, M2Date As Date, M3Date As Date
        LR = Cells(Rows.Count, "B").End(3).Row
        Range("B1").Resize(1, 3).Copy Cells(1, DstCol)
        Application.ScreenUpdating = False
        For I = 2 To LR
            QDate = Cells(I, 2)
            EmpN = Cells(I, 3)
            Totinc = Cells(I, 4)
            MonInc = Totinc / 3
            M1Date = DateSerial(Year(QDate), Month(QDate) - 1, 1) - 1
            M2Date = DateSerial(Year(QDate), Month(QDate), 1) - 1
            M3Date = DateSerial(Year(QDate), Month(QDate) + 1, 1) - 1
            Cells(Rows.Count, DstCol).End(3)(2).Resize(1, 3) = Array(M1Date, EmpN, MonInc)
            Cells(Rows.Count, DstCol).End(3)(2).Resize(1, 3) = Array(M2Date, EmpN, MonInc)
            Cells(Rows.Count, DstCol).End(3)(2).Resize(1, 3) = Array(M3Date, EmpN, MonInc)
        Next I
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    - Battle without fear gives no glory - Just try

  5. #5
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,466

    Re: VB Code to convert Quarter data into Monthly data

    Or try a formula
    Formula: copy to clipboard
    =MAKEARRAY(3*ROWS(Table3),3,LAMBDA(r,c,CHOOSE(c,EOMONTH(INDEX(Table3,(r+2)/3,c),MOD(r-1,3)-2),INDEX(Table3,(r+2)/3,c),INDEX(Table3,(r+2)/3,c)/3)))
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to convert Quarter data into Monthly data

    Hello
    Thanks to all
    I will be using PCI's code

    R

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,086

    Re: VB Code to convert Quarter data into Monthly data

    Super, while others suggestions are really great.
    Thx for the Rep

  8. #8
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: VB Code to convert Quarter data into Monthly data

    Here is another code which gives result as shown by you.
    Code
    Sub QuarterToMonthly()
    Dim A, B, C, D
    Dim Qdt As Date
    Dim T As Long, X As Long, Mth As Long
    Dim Val As Double
    If ActiveSheet.Name <> "Sheet1" Then Sheets("Sheet1").Activate
    Sheets("Sheet2").Range("B1").CurrentRegion.Offset(1, 0).ClearContents
    
    With Range("B1").CurrentRegion
    A = .Offset(1, 0).Value
    End With
    ReDim B(1 To UBound(A, 1), 1 To 3)
    ReDim C(1 To UBound(A, 1), 1 To 3)
    ReDim D(1 To UBound(A, 1), 1 To 3)
    
    With Sheets("Sheet2")
    .[B1] = "Date": .[C1] = "EmpName": .[D1] = "TotInc"
    End With
    For T = 1 To UBound(A, 1) - 1
    X = X + 1
    B(X, 1) = WorksheetFunction.EoMonth(A(T, 1), -2)
    C(X, 1) = WorksheetFunction.EoMonth(A(T, 1), -1)
    D(X, 1) = A(T, 1)
    Val = Round(A(T, 3) / 3, 2)
    B(X, 2) = A(T, 2): B(X, 3) = Val
    C(X, 2) = A(T, 2): C(X, 3) = Val
    D(X, 2) = A(T, 2): D(X, 3) = Val
    
    If T = UBound(A, 1) - 1 Or A(T, 1) <> A(T + 1, 1) Then
    
    With Sheets("Sheet2")
    .Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Resize(3 * X, 1).NumberFormat = "m/d/yyyy"
    .Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Resize(X, 3) = B
    .Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Resize(X, 3) = C
    .Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Resize(X, 3) = D
    End With
    X = 0
    
    End If
    Next T
    
    End Sub
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Convert monthly row data into single column daily data
    By chsaleem in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-29-2019, 04:17 AM
  2. Convert monthly row data into single column daily data
    By chsaleem in forum Excel General
    Replies: 3
    Last Post: 11-29-2019, 04:16 AM
  3. Replies: 11
    Last Post: 06-06-2018, 05:08 PM
  4. Excel 2007 : How to convert weekly data to monthly data
    By kissanbhai009 in forum Excel General
    Replies: 3
    Last Post: 08-10-2017, 04:35 AM
  5. Formula to convert monthly data into weekly data - help needed
    By dobrica3 in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 11-12-2016, 10:00 AM
  6. [SOLVED] Formula to convert monthly data to weekly data
    By FSmit in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 05-15-2014, 09:12 AM
  7. getting data from monthly quarter
    By andyx181x in forum Excel General
    Replies: 5
    Last Post: 09-28-2010, 12:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1