Excel to recommend 8 "Price Groups" from range of prices

sanantonio

Board Regular
Joined
Oct 26, 2021
Messages
129
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Pre-amble:
Thanks for taking the time to read my requirement. I'm convinced there's probably a real simple way of doing this but I've sat down to try and put something together a couple of times and I'm all googled out. Up to this point I've been using quartile and a kind of manual game of upping and downing. It's a massive time suction on my day for something so trivial.

INPUT:
I work with different data sets from different businesses. I get a raw range of approx 1,000+ items with their sale price. Example:
1737543263644.png


Sometimes you'll get a data set and the sales prices range between $0.49 and $9.99. Sometimes $1.00 and $99, sometimes $0.49 and $999.99.

OUTPUT:
I need to create exactly 8 "Price Groups", ideally with roughly the same amount of items in each.

For example:
1737543687980.png

(Price groups don't necessarily have to be named automatically, but maybe something like this:

1737543749849.png

(Or any combination really)

ASK:

How can I get excel to recommend price groupings to be automatically?

Can be a formulaic or VBA solution but I really need something to save me multiple hours a week with this task. Thanks!
 

Attachments

  • 1737543621387.png
    1737543621387.png
    4.5 KB · Views: 2

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You could have 8 bands of virtually identical size if you don't mind price ranges like:

$0.00 to $2.33
$2.34 to $7.55 etc

But I'm guessing you'd prefer whole dollar bands, e.g

$0.00 to $2.99
$3.00 to $5.99?

You could use Solver ...

I set up the initial guess below, based on 8 bands roughly equally spaced between $0 and $20

ABCDE
1PricesBandCount
27.250$0.00 - $1.997
316.902$2.00 - $4.9916
43.255$5.00 - $7.9915
519.908$8.00 - $10.9910
65.9011$11.00 - $13.9914
74.2514$14.00 - $16.9922
814.9917$17.00 - $19.9913
93.0020$20.00 +3
1011.90100
1119.25
1213.00Var(Count)34.0
137.90
145.00
154.99
1618.25
174.99
181.25
198.90
2010.00
211.50
2213.25
2319.00
249.50
2516.25
2619.99
2718.00
285.99
2910.99
3011.25
315.25
3216.50
333.50
344.90
3518.90
3614.50
3715.90
3819.25
3910.90
4011.50
4118.00
4210.00
4312.99
4420.00
4514.99
4615.99
4715.90
4812.50
4916.25
5011.00
517.90
5214.99
5319.99
5412.00
553.00
561.90
5714.90
581.99
5914.25
6015.90
6115.99
6211.50
636.00
642.00
659.25
6610.99
6714.00
6817.25
6913.50
702.50
711.50
7220.00
7314.25
7413.99
754.50
763.90
777.25
7811.99
795.90
801.00
816.99
822.99
8316.99
8416.99
8511.00
8617.99
874.25
886.50
899.00
902.99
917.99
926.25
932.00
9416.99
958.00
966.25
9716.25
9819.25
9920.25
1001.25
10116.50
Sheet1
Cell Formulas
RangeFormula
D2:D8D2=TEXT(C2,"$0.00")&" - " & TEXT(C3-0.01,"$0.00")
D9D9=TEXT(C9,"$0.00")&" +"
E2:E8E2=COUNTIFS(A$2:A$101,">="&C2,A$2:A$101,"<"&C3)
E9E9=COUNTIFS(A$2:A$101,">="&C9)
E10E10=SUM(E2:E9)
E12E12=VAR(E2:E9)
Named Ranges
NameRefers ToCells
solver_adj=Sheet5!$C$2:$C$9D2:E2
solver_lhs1=Sheet5!$C$2:$C$9D2:E2


Solver provided this solution:

BandCount
0$0.00 - $3.9917
4$4.00 - $5.9911
6$6.00 - $8.9912
9$9.00 - $11.9915
12$12.00 - $14.9915
15$15.00 - $16.9914
17$17.00 - $18.996
19$19.00 +10
100
Var(Count)12.3


This looks like about the best you can do with this sample data and whole dollar increments. It would be relatively simple to adapt if you were happy to vary the increment, e.g. from $1.00 to $0.50, say.

1737586518616.png
 
Upvote 0
Hi sanantonio. You can trial this. HTH. Dave
VBA Code:
Sub Group8()
'Sales prices in B2 to whatever
'output 8 price groups in E2 to whatever; No. of items in F2 to whatever
Dim LastRow As Integer, Arr As Variant, Low As Double, High As Double, Interval As Integer
Dim Cnt As Integer, Cnt2 As Integer, Cnt3 As Integer, i As Integer, Rng As Range
'****change sheet name to suit
With Sheets("Sheet1")
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
Set Rng = .Range("B2:B" & LastRow)
'change rng to array
Arr = Application.WorksheetFunction.Transpose(Rng)
'get no. of items pefr group
Interval = Round((LastRow - 1) / 8)
'output set up/ clear previous results
.Range("E" & 1).Value = "Price Group"
.Range("F" & 1).Value = "Number of Items"
.Range(.Cells(2, "E"), .Cells(9, "F")).ClearContents
'get lowest price for group
Low = Arr(GetMin(Arr))
'create 8 price groups
For Cnt = 2 To 9
'loop each interval
For Cnt2 = 1 To Interval
    'remove interval number of items from array
    For i = GetMin(Arr) + 1 To UBound(Arr) ' - 1
    Arr(i - 1) = Arr(i)
    Next i
    ReDim Preserve Arr(UBound(Arr) - 1)
Next Cnt2
'get highest remaining value for group
High = Arr(GetMin(Arr))
'output group price range
.Range("E" & Cnt).Value = Format(Low, "currency") & " - " & Format(High, "currency")
'output number ot items in each range
For Cnt3 = 1 To LastRow
If .Range("B" & Cnt3).Value >= Low And .Range("B" & Cnt3).Value <= High Then
.Range("F" & Cnt).Value = .Range("F" & Cnt).Value + 1
End If
Next Cnt3
'set low for next group
Low = High + 0.01
Next Cnt
End With
End Sub

Function GetMin(Tarr As Variant) As Integer
'returns array posn of smallest value
Dim Cnt As Integer, TempCnt As Integer, mini As Double
mini = Tarr(LBound(Tarr))
For Cnt = LBound(Tarr) To UBound(Tarr) - 1
If Tarr(Cnt) <= mini Then
mini = Tarr(Cnt)
TempCnt = Cnt
End If
Next Cnt
GetMin = TempCnt
End Function
Edit: I see that Stephen has already provided something likely better. It was fun anyways. Dave
 
Upvote 0
Solution
You could have 8 bands of virtually identical size if you don't mind price ranges like:

$0.00 to $2.33
$2.34 to $7.55 etc

But I'm guessing you'd prefer whole dollar bands, e.g

$0.00 to $2.99
$3.00 to $5.99?

You could use Solver ...

I set up the initial guess below, based on 8 bands roughly equally spaced between $0 and $20

ABCDE
1PricesBandCount
27.250$0.00 - $1.997
316.902$2.00 - $4.9916
43.255$5.00 - $7.9915
519.908$8.00 - $10.9910
65.9011$11.00 - $13.9914
74.2514$14.00 - $16.9922
814.9917$17.00 - $19.9913
93.0020$20.00 +3
1011.90100
1119.25
1213.00Var(Count)34.0
137.90
145.00
154.99
1618.25
174.99
181.25
198.90
2010.00
211.50
2213.25
2319.00
249.50
2516.25
2619.99
2718.00
285.99
2910.99
3011.25
315.25
3216.50
333.50
344.90
3518.90
3614.50
3715.90
3819.25
3910.90
4011.50
4118.00
4210.00
4312.99
4420.00
4514.99
4615.99
4715.90
4812.50
4916.25
5011.00
517.90
5214.99
5319.99
5412.00
553.00
561.90
5714.90
581.99
5914.25
6015.90
6115.99
6211.50
636.00
642.00
659.25
6610.99
6714.00
6817.25
6913.50
702.50
711.50
7220.00
7314.25
7413.99
754.50
763.90
777.25
7811.99
795.90
801.00
816.99
822.99
8316.99
8416.99
8511.00
8617.99
874.25
886.50
899.00
902.99
917.99
926.25
932.00
9416.99
958.00
966.25
9716.25
9819.25
9920.25
1001.25
10116.50
Sheet1
Cell Formulas
RangeFormula
D2:D8D2=TEXT(C2,"$0.00")&" - " & TEXT(C3-0.01,"$0.00")
D9D9=TEXT(C9,"$0.00")&" +"
E2:E8E2=COUNTIFS(A$2:A$101,">="&C2,A$2:A$101,"<"&C3)
E9E9=COUNTIFS(A$2:A$101,">="&C9)
E10E10=SUM(E2:E9)
E12E12=VAR(E2:E9)
Named Ranges
NameRefers ToCells
solver_adj=Sheet5!$C$2:$C$9D2:E2
solver_lhs1=Sheet5!$C$2:$C$9D2:E2


Solver provided this solution:

BandCount
0$0.00 - $3.9917
4$4.00 - $5.9911
6$6.00 - $8.9912
9$9.00 - $11.9915
12$12.00 - $14.9915
15$15.00 - $16.9914
17$17.00 - $18.996
19$19.00 +10
100
Var(Count)12.3


This looks like about the best you can do with this sample data and whole dollar increments. It would be relatively simple to adapt if you were happy to vary the increment, e.g. from $1.00 to $0.50, say.

View attachment 121543

Hi Stephen,

Thanks for coming back to me.

I haven't heard of "Solver" before? I've just checked my add in list and it's there. I've added it but can't seem to find it within my excel, I'll do some youtubing and see if I can fathom it out.

You're solution looks great, but how are the values in Column C populated? Is this what this "Solver" does?
 
Upvote 0
Could you post some sample data to see what, if anything, went wrong with the code I posted? It seemed to work with my limited data mock up. Dave
 
Upvote 0
Could you post some sample data to see what, if anything, went wrong with the code I posted? It seemed to work with my limited data mock up. Dave

Hi Dave,

Thanks for your response. I hadn't tried your code just yet, was replying to the previous solution.

I've just tried the VBA and it works perfectly! I get a debug if there are any none numbers here:
1737729497963.png


But it still generates the result okay, so I've thrown an on error resume next in there and it seems to be all good.

Thank you very much!
 
Upvote 0
You are welcome sanantonio. I'm hoping you're relieved by the disappearance of the time suction. I'm not quite sure what you meant by "Ignore me, sorted it! "... was there an error that needs to be addressed? Dave
 
Upvote 0
Oh for sure! It's why I love this forum so much, you've saved me countless man hours over the past few years!

And no issues with the VBA at all, I had posted that it was only returning 7 ranges, not 8, but it seems to return 7 and hit debug if column B has anything other than numbers in it. So I've added a step that removes none numbers before running the VBA!
 
Upvote 0
Thanks for the update and I'm glad you got it sorted out. I hadn't considered the possibility of non-numeric data. Have a nice day. Dave
 
Upvote 0

Forum statistics

Threads
1,226,075
Messages
6,188,735
Members
453,496
Latest member
boninm

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