VBA to Assign category numbers

BORUCH

Well-known Member
Joined
Mar 1, 2016
Messages
529
Office Version
  1. 365
Platform
  1. Windows
hi all
i have an excel sheet that looks like below

1644721216798.png


I'm looking for a VBA that would automate for me column D and assign category numbers starting with 001 002 etc..

the criteria is if they are are all the same price, but also right underneath each other so for example the item number 00001 and item number c123 are both 100 dollars but not the same group

there will always be an empty row between each group

any help is greatly appreciated

thanks
 
Thanks for the XL2BB samples. One further issue to clarify please:
Is data like this possible and, if so, what would be the category results?

BORUCH.xlsm
ABCD
1ITEM #DESCRIPTION PRICE CATEGORY RESULTS
21Chains14.60
32Socks15.00
43Bib-Shorts15.55
54Shorts15.00
65Tights14.60
76Other14.60
8
Sample
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Thanks for the XL2BB samples. One further issue to clarify please:
Is data like this possible and, if so, what would be the category results?

BORUCH.xlsm
ABCD
1ITEM #DESCRIPTION PRICE CATEGORY RESULTS
21Chains14.60
32Socks15.00
43Bib-Shorts15.55
54Shorts15.00
65Tights14.60
76Other14.60
8
Sample
Hi
It can be possible
The 14.60 would be 001 the 15 002 the 15.55 003
 
Upvote 0
I'm not really sure I understand the logic.
In post #10, row 5 was a single value in the group & was skipped.
In post #11, row 4 is a single value in the group but is not skipped.
Can you clarify the above and also advise the results for this group where there are 2 single values in the group but all other values are the same?

BORUCH.xlsm
ABC
1ITEM #DESCRIPTION PRICE
21Chains14.60
32Socks14.60
43Bib-Shorts13.00
54Shorts14.60
65Tights15.00
76Other14.60
Sample
 
Upvote 0
I'm not really sure I understand the logic.
In post #10, row 5 was a single value in the group & was skipped.
In post #11, row 4 is a single value in the group but is not skipped.
Can you clarify the above and also advise the results for this group where there are 2 single values in the group but all other values are the same?

BORUCH.xlsm
ABC
1ITEM #DESCRIPTION PRICE
21Chains14.60
32Socks14.60
43Bib-Shorts13.00
54Shorts14.60
65Tights15.00
76Other14.60
Sample
I apologize the 15.55 would be skipped sorry for that
 
Upvote 0
Try this with a copy of your workbook.
I have assumed that the values in column C are not the result of formulas.

VBA Code:
Sub AllocateCategories()
  Dim rA As Range
  
  With Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row)
    .NumberFormat = "000"
    For Each rA In .Offset(, -1).SpecialCells(xlConstants).Areas
      rA.Offset(, 1).FormulaR1C1 = "=IF(COUNTIF(" & rA.Address(, , xlR1C1) & ",RC[-1])=1,"""",IFNA(VLOOKUP(RC[-1],R" & rA.Row - 1 & "C[-1]:R[-1]C,2,0),MAX(R1C:R[-1]C)+1))"
    Next rA
    .Value = .Value
  End With
End Sub

My sample data and results:

BORUCH.xlsm
CD
1 PRICE CATEGORY RESULTS
214.60001
314.60001
413.00
514.60001
615.00
714.60001
8
9100.20002
10496.00003
11496.00003
12100.20002
13
1425.00
15
1690.00004
17100.00
1890.00004
1990.00004
2090.00004
21
2220.00005
2320.00005
24
Sample
 
Upvote 0
Try this with a copy of your workbook.
I have assumed that the values in column C are not the result of formulas.

VBA Code:
Sub AllocateCategories()
  Dim rA As Range
 
  With Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row)
    .NumberFormat = "000"
    For Each rA In .Offset(, -1).SpecialCells(xlConstants).Areas
      rA.Offset(, 1).FormulaR1C1 = "=IF(COUNTIF(" & rA.Address(, , xlR1C1) & ",RC[-1])=1,"""",IFNA(VLOOKUP(RC[-1],R" & rA.Row - 1 & "C[-1]:R[-1]C,2,0),MAX(R1C:R[-1]C)+1))"
    Next rA
    .Value = .Value
  End With
End Sub

My sample data and results:

BORUCH.xlsm
CD
1 PRICE CATEGORY RESULTS
214.60001
314.60001
413.00
514.60001
615.00
714.60001
8
9100.20002
10496.00003
11496.00003
12100.20002
13
1425.00
15
1690.00004
17100.00
1890.00004
1990.00004
2090.00004
21
2220.00005
2320.00005
24
Sample
Hi
thanks for the code
one little problem is that if my data does not start from column C rather let’s say E13 or any other cell it would not work ,the same with column D it would be helpful if i would be able to choose which columns
 
Upvote 0
Are you saying that the category results and the prices may not be in adjacent columns?

Would the rows always correspond? That is, if the 'Price' heading is in row 7 somewhere would the 'Category' heading also be in row 7 somewhere?

Need to understand just what sort of variation(s) you are contemplating. Please try to explain clearly, preferably with examples.
 
Upvote 0

Forum statistics

Threads
1,215,659
Messages
6,126,074
Members
449,286
Latest member
Lantern

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