Summary report questions

J

Joel

I have created a spreadsheet, that links to another source data spreadsheet.
The source data spreadsheet is exported from SAP (our ERP system). All the
brains are programmed in the new spreadsheet that links to the exported file.
Of all the data that gets exported, there are two columns I'm interested in,
Inventory Value and Material Group.

What I would like to do is to group by the different material groups, sum
their Inventory Values, and sort the resulting sum in descending order
through VBA code. The results would then be displayed on a chart.

One problem is that every time the data source spreadsheet is refreshed, the
number and name of Material Groups can vary. Therefore, I can't hard-code the
material group names.

Of course, the same data is used on other worksheets for other graphs as
well. I am trying to code the operations listed in the Chart_Activate( )
event, and 'undo' all the sorting and grouping in the Chart_Deactivate( )
event.

Any ideas?

Here's an example:

Source Data:

Material Group Inventory Value .........
----------------------------------------------------------
Machined Metal $495.00
Printed Circuit Assy $2,364.93
Plastic and Rubber $416.00
Sheet Metal $625.00
Sheet Metal $250.00
Printed Circuit Assy $2,364.75
Printed Circuit Assy $2,187.24
Printed Circuit Assy $155.15



Summary Report (Inv $ Sum & Sort):

Material Group Inventory Value
 
J

Jim Thomlinson

Why not use a pivot table instead of a whole pile of code. The pivot will do
that for you without any effort or code maintenance. It will handle the
aggregation and sorting along with a whole pile of other neat features.
 
T

Tom Ogilvy

You can do an advanced filter on the column of Material Groups to get a
unique list. Then use the sumif formula to get the sum. (all done with code
of course - use the macro recorder while you do it manually as a start).
 
J

Jim Thomlinson

If you want to try this way (which is a kind of cool solution) here is a way
to get a list of unique items. This sub generates a list of unique items and
pastes them onto a new sheet based on your selection on the original sheet.
This might make Tom's code idea a little easier to implement.

Private Sub GetUniqueItems()
Dim cell As Range 'Current cell in range to check
Dim rngToSearch As Range 'Cells to be searched
Dim dic As Scripting.Dictionary 'Dictionary Object
Dim dicItem As Variant 'Items within dictionary object
Dim wks As Worksheet 'Worksheet to populate with
unique items
Dim rngPaste As Range 'Cells where unique items are
placed

'Create range to be searched
Set rngToSearch = Intersect(ActiveSheet.UsedRange, Selection)

'Confirm there is a relevant range selected
If Not rngToSearch Is Nothing Then
'Create dictionay object
Set dic = New Scripting.Dictionary

'Populate dictionary object with unique items (use key to define
unique)
For Each cell In rngToSearch 'Traverse selected range
If Not dic.Exists(cell.Value) And cell.Value <> Empty Then
'Check the key
dic.Add cell.Value, cell.Value 'Add the item if unique
End If
Next

If Not dic Is Nothing Then 'Check for dictionary
Set wks = Worksheets.Add 'Create worksheet to populate
Set rngPaste = wks.Range("A1") 'Create range to populate
For Each dicItem In dic.Items 'Loop through dictionary
rngPaste.NumberFormat = "@" 'Format cell as text
rngPaste.Value = dicItem 'Add items to new sheet
Set rngPaste = rngPaste.Offset(1, 0) 'Increment paste range
Next dicItem
'Clean up objects
Set wks = Nothing
Set rngPaste = Nothing
Set dic = Nothing
End If
End If
End Sub
 
J

Jim Thomlinson

I forgot to mention to use this code you need to reference the "Microsoft
Scripting Runtime". If you need any help with that let me know...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top