+ Reply to Thread
Results 1 to 11 of 11

Summarizing Data

Hybrid View

  1. #1
    Registered User
    Join Date
    01-03-2020
    Location
    US
    MS-Off Ver
    2019
    Posts
    85

    Question Summarizing Data

    Hi,
    I have a sample excel data which has 5 columns. I want to summarize them based on IDs and Work Time. So, for each ID, I need to sum its corresponding Work Time values and at the same time, keep other corresponding data too. For better understandings, I provided my desired final solution in sheet 2 (the answer should be written in a new sheet).
    I think that I need to use VBA Dictionary.
    Please help me out (Please note that I just need VBA code, not Power Pivot).

    I wrote a code but I couldn't continue. I failed to complete the code. I'm new to VBA Dictionaries and don't have enough understandings of dictionary usage in different situation yet.
    Sub test()
        Dim a, i As Long, txt As String, dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        a = Sheets("Sheet1").Range("A1:E11")
        For i = 1 To UBound(a, 1)
            txt = Join(Array(a(i, 1), a(i, 2), a(i, 3), a(i, 4)), "~")
            dic(txt) = dic(txt) + a(i, 4)
        Next
        With Sheets.Add.Cells(1).Resize(, UBound(a, 2))
            .Value = Array("ID", "Farm", "Department", "Work Time", "Date")
    Attached Files Attached Files
    Last edited by fa2020; 08-15-2020 at 01:32 PM.

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Summarizing Data

    you can do this with code, but you might also be able to pull it off with a simple function. try putting this in any test cell on sheet1 and see if you can use it to get what you need in any place in the workbook:
    =SUMIF(A2:A11, 100, D2:D11)

  3. #3
    Registered User
    Join Date
    01-03-2020
    Location
    US
    MS-Off Ver
    2019
    Posts
    85

    Re: Summarizing Data

    This is a sample data. My real data has thousands if rows. I need to automate this procedure which its output is an input for other procedures and further calculations.
    I'm sure VBA dictionaries can do it. I tried some parts of it but I failed to continue.

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Summarizing Data

    can you post what you've already tried? the code to do this is very simple, but i would be interested to see what you've tried first. and by the way, SUMIF() will work for millions of lines of data. the count of rows is irrelevant. I just thought you could use it to your advantage.

  5. #5
    Registered User
    Join Date
    01-03-2020
    Location
    US
    MS-Off Ver
    2019
    Posts
    85

    Re: Summarizing Data

    Quote Originally Posted by vba_php View Post
    can you post what you've already tried? the code to do this is very simple, but i would be interested to see what you've tried first. and by the way, SUMIF() will work for millions of lines of data. the count of rows is irrelevant. I just thought you could use it to your advantage.
    I need to use VBA dictionar.
    Sub test()
        Dim a, i As Long, txt As String, dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        a = Sheets("Sheet1").Range("A1:E11")
        For i = 1 To UBound(a, 1)
            txt = Join(Array(a(i, 1), a(i, 2), a(i, 3), a(i, 4)), Chr(2))
            dic(txt) = dic(txt) + a(i, 4)
        Next
        With Sheets.Add.Cells(1).Resize(, UBound(a, 2))
            .Value = Array("ID", "Farm", "Department", "Work Time", "Date")
    I learned this type of code in a tutorial but the tutorial aim was something else. I tried to solve my problem by that dictionary method but I failed. I'm sure VBA dictionary can do it.
    Last edited by fa2020; 08-15-2020 at 01:45 PM.

  6. #6
    Registered User
    Join Date
    01-03-2020
    Location
    US
    MS-Off Ver
    2019
    Posts
    85

    Re: Summarizing Data

    I think I found my own answer:
    Sub test()
        Dim a, i As Long, txt As String, dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        a = Sheets("Sheet1").Range("A2:E11")
        For i = 1 To UBound(a, 1)
            txt = Join(Array(a(i, 1), a(i, 2), a(i, 3), a(i, 5)), Chr(2))
            dic(txt) = dic(txt) + a(i, 4)
        Next
        With Sheets.Add.Cells(1).Resize(, UBound(a, 2))
            .Value = Array("ID", "Farm", "Department", "Date", "Work Time")
            With .Rows(2).Resize(dic.Count)
                With .Columns(1)
                    .Value = Application.Transpose(dic.Keys)
                    .TextToColumns .Cells(1), 1, other:=True, otherchar:=Chr(2)
                End With
                .Columns(.Columns.Count).Value = Application.Transpose(dic.Items)
                .Sort .Cells(1, 1), 1, , Cells(1, 2), 1
            End With
            .CurrentRegion.Columns.AutoFit
        End With
    End Sub

  7. #7
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Summarizing Data

    ummmmm....probably don't use the dictionary object? according to microsoft, it was modeled after PERL. that might just add confusion:

    https://docs.microsoft.com/en-us/off...tionary-object

    what about this?

    Function summary()
    Dim newnum As Long
    Dim oldnum As Long
    Dim accum As Long
    Dim nextrow As Long
    Dim r As Range
    Dim ws As Worksheet
    Dim ws2 As Worksheet
    Set ws = ActiveWorkbook.Worksheets("Sheet1")
    Set ws2 = ActiveWorkbook.Worksheets("Sheet2")
    
    Application.ScreenUpdating = False
    
    nextrow = 2
    
        For Each r In ws.Range("a2", ws.Range("a2").End(xlDown))
            oldnum = CLng(Trim(r))
                If nextrow <> 0 Then
                    ws2.Range("d" & CStr(nextrow)) = "=SUMIF(Sheet1!A2:A11," & oldnum & ",Sheet1!D2:D11)"
                        If CLng(r.Offset(1, 0)) <> oldnum Then
                            newnum = CLng(r.Offset(1, 0))
                            nextrow = nextrow + 1
                        Else
                            newnum = 0
                        End If
                End If
        Next r
    
    Application.ScreenUpdating = True
    
    End Function
    that will work with the sample you uploaded. you can change the static range obviously to dynamics. I will let you take it from here.

  8. #8
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Summarizing Data

    Although it looks like you might have solved this, here is another code that may help. It's not pretty, but it worked for me on your data. I compared this outcome with that of Post#6 and found you must identify the whole range in that code, but this code can dynamically size to the data automatically.

    Option Explicit
    
    Sub filterrearrange()
    
    
    Application.ScreenUpdating = False
    Dim x, y As Range
    Dim rng, rg As Range
    Dim last, lr, i, j As Long
    Dim sht, sp As String
    Dim sum As Double
    
    'specify sheet name in which the data is stored
    sht = "Sheet1"
    
    'change filter column in the following code
    last = Sheets(sht).Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = Sheets(sht).Range("A1:E" & last)
    
    Sheets.Add(after:=Sheets(Sheets.Count)).Name = "New"
    Sheets("sheet1").Range("A1:E1").Copy Sheets("New").Range("A1")
    
    Sheets(sht).Range("A1:A" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("New").Range("A1"), Unique:=True
    
    sum = 0
        j = 2
        For Each x In Sheets("New").Range([A2], Cells(Rows.Count, "A").End(xlUp))
            With rng
                .AutoFilter
                .AutoFilter Field:=1, Criteria1:=x.Value
                .SpecialCells(xlCellTypeVisible).Copy
                Sheets.Add(after:=Sheets(Sheets.Count)).Name = x.Value
                ActiveSheet.Paste
                lr = Cells(Rows.Count, "D").End(xlUp).Row
                For i = 2 To lr
                    sum = sum + Range("d" & i)
                    Range("d2") = sum
                Next i
                sum = 0
            End With
        Next x
        
    With ThisWorkbook.Sheets("New")
        .Select
         Range("A1").Select
    End With
    
        'With Sheets("New")
        For Each y In Sheets("New").Range([A2], Cells(Rows.Count, "A").End(xlUp))
                 Sheets("New").Range("B" & j).Value = Sheets(y.Text).Range("B2").Value
                 Sheets("New").Range("C" & j).Value = Sheets(y.Text).Range("C2").Value
                 Sheets("New").Range("D" & j).Value = Sheets(y.Text).Range("D2").Value
                 Sheets("New").Range("E" & j).Value = Sheets(y.Text).Range("E2").Value
            j = j + 1
            Application.DisplayAlerts = False
            Sheets(y.Text).Delete
            Application.DisplayAlerts = True
        Next y
        'End With
        ' Turn off filter
        Sheets(sht).AutoFilterMode = False
        
        Application.ScreenUpdating = True
    End Sub
    Last edited by maniacb; 08-15-2020 at 11:49 PM. Reason: Corrections to response

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Summarizing Data

    Quote Originally Posted by fa2020 View Post
    I wrote a code but I couldn't continue. I failed to complete the code. I'm new to VBA Dictionaries and don't have enough understandings of dictionary usage in different situation yet.
    Did you?
    https://www.excelforum.com/excel-pro...-a-report.html

  10. #10
    Registered User
    Join Date
    01-03-2020
    Location
    US
    MS-Off Ver
    2019
    Posts
    85

    Re: Summarizing Data

    Quote Originally Posted by jindon View Post
    I asked similar question before, but I made some changes to correspond this new problem. Actually, you helped me
    Last edited by fa2020; 08-16-2020 at 09:03 AM.

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Summarizing Data

    Your result of 103 should be 5 not 3.9...
    Sub sintek()
    Dim Data, Dict As Object, Id As String, i As Long
    Set Dict = CreateObject("Scripting.Dictionary")
    Data = Sheet1.Cells(1).CurrentRegion
    For i = 2 To UBound(Data)
        Id = Data(i, 1)
        If Not Dict.Exists(Id) Then
            Dict(Id) = Array(Id, Data(i, 2), Data(i, 3), Data(i, 4), Data(i, 5))
        Else
            Dict(Id) = Array(Dict(Id)(0), Dict(Id)(1), Dict(Id)(2), Dict(Id)(3) + Data(i, 4), Dict(Id)(4))
        End If
    Next i
    With Sheets.Add
        .Range("A1").Resize(, 5) = Sheet1.Range("A1").Resize(, 5).Value
        .Range("A2").Resize(Dict.Count, 5) = Application.Transpose(Application.Transpose(Dict.items))
        .UsedRange.Borders.LineStyle = xlContinuous
    End With
    End Sub
    Last edited by Sintek; 08-16-2020 at 05:42 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

+ 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. [SOLVED] Summarizing data
    By icevinson in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-19-2015, 08:47 PM
  2. Help with summarizing data
    By ppalmerirs in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-05-2014, 09:15 AM
  3. Data summarizing!?
    By Crebsington in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-18-2011, 02:10 AM
  4. Summarizing data
    By Just James in forum Excel General
    Replies: 2
    Last Post: 04-27-2011, 07:54 AM
  5. Summarizing Data
    By C90RanMan in forum Excel General
    Replies: 11
    Last Post: 06-10-2010, 02:30 PM
  6. Summarizing raw data
    By sonyap in forum Excel General
    Replies: 1
    Last Post: 10-31-2009, 09:48 AM
  7. Summarizing data
    By Pedro AM in forum Excel General
    Replies: 1
    Last Post: 07-03-2006, 03:35 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