Technology Get the latest on technology, electronics and software…

SUMIF, DSUM, WTF (Excel anyone?)

Thread Tools
 
Old 06-22-2009, 12:40 PM
  #1  
I hate the Mets
Thread Starter
 
mc222's Avatar
 
Join Date: Jun 2002
Location: Philly Burbs
Age: 45
Posts: 4,994
Likes: 0
Received 0 Likes on 0 Posts
SUMIF, DSUM, WTF (Excel anyone?)

If you need to do more than a pivot table in excel you have lost me...So when a colleague asked me to do this, I said "I'll see what I can do".

If one of you guys knows how to help, I would appreciate it...

The screen shot below is a completely dynamic table - There are many diferent values that can be added to it, changed, etc - so any solution cannot be tied to static data.

I need to create a new tab, that takes values out of column E and summarizes them so next to each K# or L# the proper number is summed up...

So in this case L6 = 222 based on column C and 255 based on column D

The tricky part is that it is completely dynamic and Columns A and B are always changing....

Time to break open the Excel help book....
Old 06-22-2009, 12:54 PM
  #2  
Needs more Lemon Pledge
 
stogie1020's Avatar
 
Join Date: Mar 2005
Location: Phoenix, AZ
Age: 51
Posts: 52,768
Received 2,000 Likes on 1,173 Posts
You sank my battleship.
Old 06-22-2009, 12:57 PM
  #3  
Suzuka Master
 
CUNextTuesday's Avatar
 
Join Date: May 2006
Location: off the grid
Age: 37
Posts: 5,217
Received 150 Likes on 109 Posts
You may want to reword that.. I have no clue wtf you're asking.
Old 06-22-2009, 01:06 PM
  #4  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
Are you saying you can't reference C134 and D134 in your screenshot example because when you change the dropdowns in A6 and B6 the reference may not point to the summary value? i.e. it may be C120 and D120?

In that case, you may have to resort to using VBA to run down your A column looking for a known text value or formatting key to identify it as a summary row. I've seen similar cases where people want to format summary rows (http://excel.tips.net/Pages/T002984_...otal_Rows.html) or if you want to idenfiy them for later processing (http://excelusergroup.org/forums/p/1314/4065.aspx).
Old 06-22-2009, 01:07 PM
  #5  
is learning to moonwalk i
 
moeronn's Avatar
 
Join Date: Feb 2004
Location: SoCal
Posts: 15,520
Received 3 Likes on 2 Posts
I don't even understand pivot tables, so good luck with that.
Old 06-22-2009, 01:21 PM
  #6  
I hate the Mets
Thread Starter
 
mc222's Avatar
 
Join Date: Jun 2002
Location: Philly Burbs
Age: 45
Posts: 4,994
Likes: 0
Received 0 Likes on 0 Posts
Sorry for being confusing...When I say "dynamic" , I mean to say that Column A lists different phases. These phases may change. Column B is a resource assigned to that Phase, these resources may change.

And when I say that the values may change, there may be different resources assigned, there may be more resources assigned or perhaps less resources assigned.
Old 06-22-2009, 01:32 PM
  #7  
Suzuka Master
 
CUNextTuesday's Avatar
 
Join Date: May 2006
Location: off the grid
Age: 37
Posts: 5,217
Received 150 Likes on 109 Posts
I understand static vs dynamic lol.. not sure where you're getting 222 or 255. I probably have no idea how to do this anyway - slow day at work I'd be willing to fuck around with it for a while.
Old 06-22-2009, 01:38 PM
  #8  
I hate the Mets
Thread Starter
 
mc222's Avatar
 
Join Date: Jun 2002
Location: Philly Burbs
Age: 45
Posts: 4,994
Likes: 0
Received 0 Likes on 0 Posts
Originally Posted by CUNextTuesday
I understand static vs dynamic lol.. not sure where you're getting 222 or 255. I probably have no idea how to do this anyway - slow day at work I'd be willing to fuck around with it for a while.

If you look at Column E there are different Values - I was adding up all the "Person Days w/o Cont" and "Person Days" where there was a K6.
Old 06-22-2009, 02:04 PM
  #9  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
Do you want to sum columns C and D grouping by column E?
Old 06-22-2009, 02:21 PM
  #10  
I hate the Mets
Thread Starter
 
mc222's Avatar
 
Join Date: Jun 2002
Location: Philly Burbs
Age: 45
Posts: 4,994
Likes: 0
Received 0 Likes on 0 Posts
Originally Posted by Anachostic
Do you want to sum columns C and D grouping by column E?
That "sums" it up. Yes
Old 06-22-2009, 02:46 PM
  #11  
Suzuka Master
 
CUNextTuesday's Avatar
 
Join Date: May 2006
Location: off the grid
Age: 37
Posts: 5,217
Received 150 Likes on 109 Posts
Originally Posted by mc222
If you look at Column E there are different Values - I was adding up all the "Person Days w/o Cont" and "Person Days" where there was a K6.
You had originally said L6, which is why I was confused

Can't ya use some kinda loop to check if there's a value in a certain box?
Old 06-22-2009, 03:03 PM
  #12  
I drive a Subata.
iTrader: (1)
 
JS + XES's Avatar
 
Join Date: Apr 2005
Location: Socal
Age: 39
Posts: 20,301
Received 2,603 Likes on 1,571 Posts
wtf is this
Old 06-22-2009, 06:20 PM
  #13  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
Originally Posted by mc222
That "sums" it up. Yes
And you want this in a new worksheet on in a block on the same worksheet? Most people refer to worksheets as "tabs".
Old 06-23-2009, 12:38 PM
  #14  
I hate the Mets
Thread Starter
 
mc222's Avatar
 
Join Date: Jun 2002
Location: Philly Burbs
Age: 45
Posts: 4,994
Likes: 0
Received 0 Likes on 0 Posts
Originally Posted by Anachostic
And you want this in a new worksheet on in a block on the same worksheet? Most people refer to worksheets as "tabs".
Gotcha. Yes, in a new tab.
Old 06-24-2009, 09:34 AM
  #15  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
This should do what you want. It's not elegant, but it should be functional. You'll need to change the names of the worksheets referenced in the code, but assuming your columns are C, D, and E, the rest should work for you.

Paste this into the VBA editor (Alt-F11), then go under macros and run the new macro. One more thing. You need to add a reference to Microsoft.Scripting.Runtime. This is found under the Tools>References menu. Otherwise "Dictionary" will not be a known data type.

Code:
Public Sub GroupByCol()
    Dim dict As New Dictionary
    Dim colToGroupBy As String
    Dim columnTotals As String
    Dim colValues() As String
    Dim sourceWorksheet As Worksheet
    Dim targetWorksheet As Worksheet
    Dim r As Long
    Dim c As Long
    
    ' Change these to the real names of worksheets
    Set sourceWorksheet = Me.Worksheets("Sheet1")
    Set targetWorksheet = Me.Worksheets("Sheet2")
    ' Change this to the column that has the grouping keys
    colToGroupBy = "E"
    
    ' Loop through all rows
    For r = 1 To sourceWorksheet.Rows.Count
        ' If we hit a blank row, we're at the end of the sheet, so stop summing
        If CStr(sourceWorksheet.Cells(r, "C")) = "" _
            And CStr(sourceWorksheet.Cells(r, "D")) = "" _
            And CStr(sourceWorksheet.Cells(r, "E")) = "" Then Exit For
    
        ' Either get the current totals for the group or create a new entry for totals
        If dict.Exists(CStr(sourceWorksheet.Cells(r, colToGroupBy))) Then
           columnTotals = dict(CStr(sourceWorksheet.Cells(r, colToGroupBy)))
           colValues = Split(columnTotals, ",")
        
        Else
            columnTotals = "0,0"
            colValues = Split(columnTotals, ",")
            dict.Add CStr(sourceWorksheet.Cells(r, colToGroupBy)), columnTotals
        
        End If
        
        ' Sum up the new values
        colValues(0) = CLng(colValues(0)) + CLng(sourceWorksheet.Cells(r, "C"))
        colValues(1) = CLng(colValues(1)) + CLng(sourceWorksheet.Cells(r, "D"))
        
        ' Store the update totals
        dict(CStr(sourceWorksheet.Cells(r, colToGroupBy))) = Join(colValues, ",")
        
    Next
    
    ' Switch to the sheet to hold the summaries
    targetWorksheet.Activate
    ' Clear the sheet
    targetWorksheet.Rows.Delete
    
    ' Loop through each group and write out the summaries
    For r = 0 To dict.Count - 1
        colValues = Split(dict.Items(r), ",")
        
        With targetWorksheet
            .Cells(r + 1, 1) = dict.Keys(r)
            .Cells(r + 1, 2) = colValues(0)
            .Cells(r + 1, 3) = colValues(1)
        End With
        
    Next
    
End Sub

Last edited by Anachostic; 06-24-2009 at 09:37 AM. Reason: Reference
Old 06-24-2009, 12:32 PM
  #16  
T-Swzy
 
Razzi's Avatar
 
Join Date: Jun 2007
Location: Saint Paul, MN
Age: 36
Posts: 2,575
Likes: 0
Received 0 Likes on 0 Posts
did you just whip that up 'chostic? cause that's fucking BA haha
Old 06-24-2009, 01:06 PM
  #17  
I hate the Mets
Thread Starter
 
mc222's Avatar
 
Join Date: Jun 2002
Location: Philly Burbs
Age: 45
Posts: 4,994
Likes: 0
Received 0 Likes on 0 Posts
Originally Posted by Anachostic
This should do what you want. It's not elegant, but it should be functional. You'll need to change the names of the worksheets referenced in the code, but assuming your columns are C, D, and E, the rest should work for you.

Paste this into the VBA editor (Alt-F11), then go under macros and run the new macro. One more thing. You need to add a reference to Microsoft.Scripting.Runtime. This is found under the Tools>References menu. Otherwise "Dictionary" will not be a known data type.

Code:
Public Sub GroupByCol()
    Dim dict As New Dictionary
    Dim colToGroupBy As String
    Dim columnTotals As String
    Dim colValues() As String
    Dim sourceWorksheet As Worksheet
    Dim targetWorksheet As Worksheet
    Dim r As Long
    Dim c As Long
 
    ' Change these to the real names of worksheets
    Set sourceWorksheet = Me.Worksheets("Sheet1")
    Set targetWorksheet = Me.Worksheets("Sheet2")
    ' Change this to the column that has the grouping keys
    colToGroupBy = "E"
 
    ' Loop through all rows
    For r = 1 To sourceWorksheet.Rows.Count
        ' If we hit a blank row, we're at the end of the sheet, so stop summing
        If CStr(sourceWorksheet.Cells(r, "C")) = "" _
            And CStr(sourceWorksheet.Cells(r, "D")) = "" _
            And CStr(sourceWorksheet.Cells(r, "E")) = "" Then Exit For
 
        ' Either get the current totals for the group or create a new entry for totals
        If dict.Exists(CStr(sourceWorksheet.Cells(r, colToGroupBy))) Then
           columnTotals = dict(CStr(sourceWorksheet.Cells(r, colToGroupBy)))
           colValues = Split(columnTotals, ",")
 
        Else
            columnTotals = "0,0"
            colValues = Split(columnTotals, ",")
            dict.Add CStr(sourceWorksheet.Cells(r, colToGroupBy)), columnTotals
 
        End If
 
        ' Sum up the new values
        colValues(0) = CLng(colValues(0)) + CLng(sourceWorksheet.Cells(r, "C"))
        colValues(1) = CLng(colValues(1)) + CLng(sourceWorksheet.Cells(r, "D"))
 
        ' Store the update totals
        dict(CStr(sourceWorksheet.Cells(r, colToGroupBy))) = Join(colValues, ",")
 
    Next
 
    ' Switch to the sheet to hold the summaries
    targetWorksheet.Activate
    ' Clear the sheet
    targetWorksheet.Rows.Delete
 
    ' Loop through each group and write out the summaries
    For r = 0 To dict.Count - 1
        colValues = Split(dict.Items(r), ",")
 
        With targetWorksheet
            .Cells(r + 1, 1) = dict.Keys(r)
            .Cells(r + 1, 2) = colValues(0)
            .Cells(r + 1, 3) = colValues(1)
        End With
 
    Next
 
End Sub
Awesome - I got the scripting to work! THANK YOU!

I also found a way to do it right in Excel. I used a bunch of SUMIF statements:

ie: =SUMIF(E6:E214,"K4",C6:C214)

So It searched Column E, rows 6:214 for the string K4 and then Summed up all the numbers in C6:C214 where it found K4.

So I was able to get all the L and K rates I needed.

I thank you very very much!
Old 06-24-2009, 02:32 PM
  #18  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
Originally Posted by Razzi
did you just whip that up 'chostic? cause that's fucking BA haha
That's pretty much my job. But VBA is painful for me to write in because it's older syntax and you have to do some stuff the hard way. On the other hand, you can cheat on other things that the newer languages enforce.

The problem is, sure, I can write all this code to brute force it, but then some Excel guru comes along and gives the solution in a single Excel formula. Then I look pretty lame.
Old 06-24-2009, 04:43 PM
  #19  
T-Swzy
 
Razzi's Avatar
 
Join Date: Jun 2007
Location: Saint Paul, MN
Age: 36
Posts: 2,575
Likes: 0
Received 0 Likes on 0 Posts
i always thought Excel was limited to pie charts. gawd damn
Old 06-24-2009, 05:44 PM
  #20  
sup
 
ViperrepiV's Avatar
 
Join Date: Jan 2004
Location: NYC
Age: 41
Posts: 2,147
Likes: 0
Received 1 Like on 1 Post
dat's some excel voodoo
Old 06-24-2009, 08:05 PM
  #21  
It's the Halladay season!
iTrader: (5)
 
hANDYcaptd's Avatar
 
Join Date: Mar 2008
Location: Philly, PA
Age: 36
Posts: 4,533
Received 110 Likes on 87 Posts
I use an Excel sheet with a pivot table everyday.

Never looked to see how it worked though...
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Yumcha
Automotive News
70
12-07-2020 05:39 PM
Yumcha
Automotive News
4
08-15-2019 12:58 PM
08_UA7_Gr33k
Member Cars for Sale
13
02-11-2016 02:17 PM
InFaMouSLink
Car Parts for Sale
6
10-27-2015 06:52 PM
08_UA7_Gr33k
Member Cars for Sale
1
09-27-2015 01:56 PM



Quick Reply: SUMIF, DSUM, WTF (Excel anyone?)



All times are GMT -5. The time now is 05:20 PM.