Funky Summarizing

SandsB

Well-known Member
Joined
Feb 13, 2007
Messages
705
Office Version
  1. 365
Platform
  1. Windows
Somebody posted a question about stock charts and I realized I have the same problem. I think I'm stating it better than the original post.
Every day I manually do this. I'm sure there's a better way. I've looked all over Pivot Tables and Subtotals and .....but just don't see how to do what I need. I want something that will read all of my data and give me output in a table format.

My data has these fields:
Date, starting volume, ending volume, refill volume, and some other stuff I don't care about. As in...
1/1/14, 412, 422, 418, other stuff
1/1/14, 82, 45, 912, other stuff
1/1/14, 54, 40, 28, other stuff
1/20/14, 9, 22, 20, other stuff
1/20/14. 111, 222, 333, other stuff

What I need is way to summarize all this to show me
Date, lowest starting volume on this date, highest ending volume on this date, highest refill volume on this date, lowest refill volume on this date.
1/1/14, 54, 422, 912, 28
1/20/14, 9, 222, 333, 20

Is there a way to do this with a pivot table? By magic?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
SandsB,

With your raw data already sorted/grouped by the date in column A:

How about a macro solution that will adjust for a varying number of rows, and, columns?

Sample raw data:


Excel 2007
ABCDEFGHIJK
1Datestarting volumeending volumerefill volumeother stuff
21/1/14412422418other stuff
31/1/148245912other stuff
41/1/14544028other stuff
51/20/1492220other stuff
61/20/14111222333other stuff
7
Sheet1


After the macro:


Excel 2007
ABCDEFGHIJK
1Datestarting volumeending volumerefill volumeother stuffDatelowest starting volume on this Datehighest ending volume on this Datehighest refill volume on this Datelowest refill volume on this Date
21/1/14412422418other stuff1/1/20145442291228
31/1/148245912other stuff1/20/2014922233320
41/1/14544028other stuff
51/20/1492220other stuff
61/20/14111222333other stuff
7
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, and, function
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgData()
' hiker95, 09/03/2014, ME803099
Dim r As Long, lr As Long, lc As Long, luc As Long, nlr As Long, n As Long
Dim o As Variant, j As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
lc = Cells(1, 1).End(xlToRight).Column
luc = Cells(1, Columns.Count).End(xlToLeft).Column
If luc > lc Then
  Columns(lc + 2).Resize(, luc - lc + 1).ClearContents
End If
Cells(1, lc + 2) = "Date"
Cells(1, lc + 3) = "lowest" & vbLf & "starting" & vbLf & "volume" & vbLf & "on this" & vbLf & "Date"
Cells(1, lc + 4) = "highest" & vbLf & "ending" & vbLf & "volume" & vbLf & "on this" & vbLf & "Date"
Cells(1, lc + 5) = "highest" & vbLf & "refill" & vbLf & "volume" & vbLf & "on this" & vbLf & "Date"
Cells(1, lc + 6) = "lowest" & vbLf & "refill" & vbLf & "volume" & vbLf & "on this" & vbLf & "Date"
With Range(Cells(1, lc + 2), Cells(1, lc + 6))
  .HorizontalAlignment = xlCenter
  .Columns.AutoFit
End With
Rows(1).AutoFit
nlr = CountUnique(Range("A2:A" & lr))
ReDim o(1 To nlr, 1 To 5)
For r = 2 To lr
  n = Application.CountIf(Columns(1), Cells(r, 1).Value)
  j = j + 1
  o(j, 1) = Cells(r, 1)
  o(j, 2) = WorksheetFunction.Min(Range("B" & r & ":B" & r + n - 1))
  o(j, 3) = WorksheetFunction.Max(Range("C" & r & ":C" & r + n - 1))
  o(j, 4) = WorksheetFunction.Max(Range("D" & r & ":D" & r + n - 1))
  o(j, 5) = WorksheetFunction.Min(Range("D" & r & ":D" & r + n - 1))
  r = r + n - 1
Next r
Cells(2, lc + 2).Resize(UBound(o, 1), UBound(o, 2)) = o
Columns(lc + 2).Resize(, luc - lc + 2).AutoFit
Application.ScreenUpdating = False
End Sub
Function CountUnique(ByVal Rng As Range) As Long
' Juan Pablo González, MrExcel MVP, 05/09/2003
' http://www.mrexcel.com/forum/excel-questions/48385-need-count-unique-items-column-visual-basic-applications.html
Dim St As String
Set Rng = Intersect(Rng, Rng.Parent.UsedRange)
St = "'" & Rng.Parent.Name & "'!" & Rng.Address(False, False)
CountUnique = Evaluate("SUM(IF(LEN(" & St & "),1/COUNTIF(" & St & "," & St & ")))")
End Function

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgData macro.
 
Upvote 0
SandsB,

Be back soon to update the macro to adjust the dates in the output area.
 
Upvote 0
SandsB,

After the updated macro:


Excel 2007
ABCDEFGHIJK
1Datestarting volumeending volumerefill volumeother stuffDatelowest starting volume on this Datehighest ending volume on this Datehighest refill volume on this Datelowest refill volume on this Date
21/1/14412422418other stuff1/1/145442291228
31/1/148245912other stuff1/20/14922233320
41/1/14544028other stuff
51/20/1492220other stuff
61/20/14111222333other stuff
7
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below NEW code, and, NEW function
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgData_V2()
' hiker95, 09/03/2014, ME803099
Dim r As Long, lr As Long, lc As Long, luc As Long, nlr As Long, n As Long
Dim o As Variant, j As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
lc = Cells(1, 1).End(xlToRight).Column
luc = Cells(1, Columns.Count).End(xlToLeft).Column
If luc > lc Then
  Columns(lc + 2).Resize(, luc - lc + 1).ClearContents
End If
Cells(1, lc + 2) = "Date"
Cells(1, lc + 3) = "lowest" & vbLf & "starting" & vbLf & "volume" & vbLf & "on this" & vbLf & "Date"
Cells(1, lc + 4) = "highest" & vbLf & "ending" & vbLf & "volume" & vbLf & "on this" & vbLf & "Date"
Cells(1, lc + 5) = "highest" & vbLf & "refill" & vbLf & "volume" & vbLf & "on this" & vbLf & "Date"
Cells(1, lc + 6) = "lowest" & vbLf & "refill" & vbLf & "volume" & vbLf & "on this" & vbLf & "Date"
With Range(Cells(1, lc + 2), Cells(1, lc + 6))
  .HorizontalAlignment = xlCenter
  .Columns.AutoFit
End With
Rows(1).AutoFit
nlr = CountUniqueV2(Range("A2:A" & lr))
ReDim o(1 To nlr, 1 To 5)
For r = 2 To lr
  n = Application.CountIf(Columns(1), Cells(r, 1).Value)
  j = j + 1
  o(j, 1) = Cells(r, 1)
  o(j, 2) = WorksheetFunction.Min(Range("B" & r & ":B" & r + n - 1))
  o(j, 3) = WorksheetFunction.Max(Range("C" & r & ":C" & r + n - 1))
  o(j, 4) = WorksheetFunction.Max(Range("D" & r & ":D" & r + n - 1))
  o(j, 5) = WorksheetFunction.Min(Range("D" & r & ":D" & r + n - 1))
  r = r + n - 1
Next r
Cells(2, lc + 2).Resize(UBound(o, 1), UBound(o, 2)) = o
Range(Cells(2, lc + 2), Cells(2 + n, lc + 2)).NumberFormat = "m/d/yy"
Columns(lc + 2).Resize(, luc - lc + 2).AutoFit
Application.ScreenUpdating = False
End Sub
Function CountUniqueV2(ByVal Rng As Range) As Long
' Juan Pablo González, MrExcel MVP, 05/09/2003
' http://www.mrexcel.com/forum/excel-questions/48385-need-count-unique-items-column-visual-basic-applications.html
Dim St As String
Set Rng = Intersect(Rng, Rng.Parent.UsedRange)
St = "'" & Rng.Parent.Name & "'!" & Rng.Address(False, False)
CountUniqueV2 = Evaluate("SUM(IF(LEN(" & St & "),1/COUNTIF(" & St & "," & St & ")))")
End Function

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgData_V2 macro.
 
Upvote 0
Sweet!
Thank you, thank you, thank you
 
Upvote 0
SandsB,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top