+ Reply to Thread
Results 1 to 5 of 5

Thread: Concatenate Cells Based on Dates and Sum Totals

  1. #1
    Registered User
    Join Date
    08-04-2011
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    3

    Question Concatenate Cells Based on Dates and Sum Totals

    Hi,

    I need some help with creating a macro using VBA code. Basically I have a spreadsheet that has transactional data and I would like to aggregate it by date and have one row per each date. What I have is below:

    Sales Rep ID, Date Entered, Occurence, Total Revenue
    32113, 7/1/2011, 1, $1.00
    32113, 7/1/2011, 1, $20.00
    32113, 7/1/2011, 2, $5.00
    32113, 7/1/2011, 2, $0.00
    32113, 6/28/2011, 2, $8.00
    32113, 6/28/2011, 2, $0.00

    AND the result I need to get to with the VBA code is below:

    Sales Rep ID, Date Entered, Occurence , Total Revenue
    32113, 7/1/2011, 6, $26.00
    32113, 6/28/2011, 4, $8.00



    Thank You
    Neha Gulati
    Last edited by Mordred; 08-09-2011 at 05:20 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Concatenate Cells Based on Dates and Sum Totals

    Hello NGNG,

    This macro will condense the transactions by rep and date. Copy and paste this code into a separate VBA module in your workbook. The macro assumes the data is on "Sheet1" and starts in cell "A2". You can change the worksheeet name and the starting cell if you need to. They are marked in bold.
    Sub CondenseTransactions()
    
     ' Thread: http://www.excelforum.com/excel-programming/787581-creating-a-macro-using-vba-code.html
     ' Poster: NGNG
     ' Author: Leith Ross
     
      Dim Cell As Range
      Dim Entry As Variant
      Dim ID As Variant
      Dim R As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim TAL As Object     'TransAction Log
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Sheet1")
        Set Rng = Wks.Range("A2")
        
          Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
          If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
        
          Set TAL = CreateObject("Scripting.Dictionary")
          TAL.CompareMode = vbTextCompare
          
            For Each Cell In Rng
              ID = Trim(Cell) & "," & Trim(Cell.Offset(0, 1))
              If ID <> "," Then
                If Not TAL.Exists(ID) Then
                   ReDim Entry(1)
                     Entry(0) = Cell.Offset(0, 2)
                     Entry(1) = Cell.Offset(0, 3)
                   TAL.Add ID, Entry
                Else
                   Entry = TAL(ID)
                     Entry(0) = Entry(0) + Cell.Offset(0, 2)
                     Entry(1) = Entry(1) + Cell.Offset(0, 3)
                   TAL(ID) = Entry
                End If
              End If
            Next Cell
          
          Rng.Resize(ColumnSize:=4).ClearContents
          
          Application.ScreenUpdating = False
            For Each ID In TAL
              Rng.Resize(1, 2).Offset(R, 0) = Split(ID, ",")
              Rng.Offset(R, 2).Resize(1, 2) = TAL(ID)
              R = R + 1
            Next ID
          Application.ScreenUpdating = True
         
    End Sub


    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click

    the macro's name to Run it.
    Last edited by Leith Ross; 08-09-2011 at 05:56 PM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    08-04-2011
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Concatenate Cells Based on Dates and Sum Totals

    Hi - Thanks for the reply. My requirements have changed a bit. Below is the raw data I will begin with and the sample result set I need to get to on a separate worksheet using VB code. The requirements are to count unique number of Account Numbers per Date per ID where Work Orders Types = "IN" or "UP" and Revenue > 0:

    Name ID Account Number Dwelling Type Work Order Type Customer Name Date Entered MRR Total Revenue
    Doe, John 54321 570000008 B IN dsfsf 6/28/2011 $0.00 $1.00
    Doe, John 54321 570000008 B UP dsfsf 6/28/2011 $0.00 $2.00
    Doe, John 54321 100000999 B UP dsfsf 6/29/2011 $0.00 $0.00
    Doe, John 54321 560000007 B UP lkjghj 7/1/2011 $0.00 $7.00
    Smith, Mary 12345 594800001 B DI ouiuo 5/11/2011 $0.00 $23.00
    Smith, Mary 12345 200300002 B UP bhjhj 6/23/2011 $0.00 $24.00
    Smith, Mary 12345 460000004 B IN fgjdkrfgjl 6/21/2011 $0.00 $26.00
    Smith, Mary 12345 280000008 B IN fgjdkrfgjl 6/21/2011 $0.00 $30.00
    Smith, Mary 12345 124600000 B DI ertert 5/19/2011 $0.00 $31.00

    Results should be:

    Sales Rep ID Date Entered Account Number Count
    54321 6/28/2011 1
    54321 6/29/2011 0
    54321 7/1/2011 1
    12345 6/23/2011 1
    12345 6/21/2011 2
    12345 5/11/2011 0
    12345 5/19/2011 0

  4. #4
    Registered User
    Join Date
    08-04-2011
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Concatenate Cells Based on Dates and Sum Totals

    Thanks for the reply. My requirements have changed a bit. Attached is the raw data I will begin with and the sample result set I need to get to on a separate worksheet using VB code. The requirements are to count unique number of 'Account Numbers' per 'Date' per 'ID' where 'Work Orders Types' = "IN" or "UP" and 'Total Revenue' > 0:
    Attached Files Attached Files
    Last edited by NGNG; 08-19-2011 at 06:40 PM.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Concatenate Cells Based on Dates and Sum Totals

    Hello NGNG,

    I made the changes to the macro and have tested it. It appears to work on the sample but the real test will be on the actual data. Here is the macro which has been added to the attached workbook.

    Sub CondenseTransactions()
    
     ' Thread: http://www.excelforum.com/excel-programming/787581-creating-a-macro-using-vba-code.html
     ' Poster: NGNG
     ' Author: Leith Ross
     ' Updated: August 18, 2011
     
     ' The requirements are to count unique number of Account Numbers ("B") per Date ("G") per ID ("C")
     ' where Work Orders Types ("E") = "IN" or "UP" and Revenue ("I") > 0:
     
      Dim Cell As Range
      Dim Entry As Variant
      Dim ID As Variant
      Dim R As Long
      Dim ResRng As Range
      Dim ResWks As Worksheet
      Dim RawRng As Range
      Dim RngEnd As Range
      Dim RawWks As Worksheet
      Dim TAL As Object         'TransAction Log
      
      
      ' Assign the worksheets and their starting ranges
        Set RawWks = Worksheets("Raw Data")
        Set RawRng = RawWks.Range("B2")
        
        Set ResWks = Worksheets("Result")
        Set ResRng = ResWks.Range("A2")
        
        ' Find the cell of the last ID entered
          Set RngEnd = RawWks.Cells(Rows.Count, RawRng.Column).End(xlUp)
          If RngEnd.Row < RawRng.Row Then Exit Sub Else Set RawRng = RawWks.Range(RawRng, RngEnd)
        
          Set TAL = CreateObject("Scripting.Dictionary")
          TAL.CompareMode = vbTextCompare
          
          ' IDs are in column "B" on RawData worksheet
            For Each Cell In RawRng.Cells
            
              Addx1 = Cell.Address
              
              ' Combine ID and Account Number and Entry Date separated by commas
                ID = Trim(Cell) & "," & Trim(Cell.Offset(0, 1)) & Cell.Offset(0, 5).Value
                
              ' Check there is an ID and Account Number present
                If ID <> "," Then
                 
                 ' Is this a new ID ?
                    If Not TAL.Exists(ID) Then
                      ' Yes
                        ReDim Entry(1)
                          Entry(0) = Cell.Offset(0, 5).Value    ' Entry Date
                          Entry(1) = 0                          ' Unique count
                        TAL.Add ID, Entry
                    Else
                      ' No
                        Addx2 = Cell.Address
                        Entry = TAL(ID)
                    End If
                    
                    If (Cell.Offset(0, 3) = "IN" Or Cell.Offset(0, 3) = "UP") And Cell.Offset(0, 7) > 0 Then
                       Entry(1) = Entry(1) + 1
                    End If
                    
                  TAL(ID) = Entry
                    
                End If
                
            Next Cell
          
        ' Clear the Result worksheet except for the headers in row 1
          ResWks.UsedRange.Offset(1, 0).ClearContents
          
        ' Copy the results to the Result worksheet
          Application.ScreenUpdating = False
            For Each ID In TAL
              ResRng.Offset(R, 0) = Split(ID, ",")(0)
              ResRng.Offset(R, 1).Resize(1, 2) = TAL(ID)
              R = R + 1
            Next ID
          Application.ScreenUpdating = True
         
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0