VBA to Assign category numbers

BORUCH

Well-known Member
Joined
Mar 1, 2016
Messages
528
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
 
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.
yes so the formula works perfect if everything is in the right column but sometimes it can change i will attach a sample

Book1
ABCDEFGHIJKLMNO
1ITEM #DESCRIPTION PRICE CATEGORY RESULTS ITEM #DESCRIPTION PRICE CATEGORY RESULTS
20001Chains$ 14.600001Chains$ 14.60
30001Socks$ 14.600001Socks$ 14.60
40001Bib-Shorts$ 14.600001Bib-Shorts$ 14.60
50001Shorts$ 15.000001Shorts$ 15.00
60001Tights$ 14.600001Tights$ 14.60
7
80006Brakes$ 100.200006Brakes$ 100.20
9A123ACargo Bike$ 496.00A123ACargo Bike$ 496.00
10A124Lights$ 496.00A124Lights$ 496.00
11A125Locks$ 100.20A125Locks$ 100.20
12
130007Jerseys$ 25.000007Jerseys$ 25.00
14
15B123Pumps$ 90.00B123Pumps$ 90.00
16B456Vests$ 90.00B456Vests$ 90.00
17B457Road Bikes$ 90.00B457Road Bikes$ 90.00
18G514Pedals$ 90.00G514Pedals$ 90.00
19123456Helmets$ 90.00123456Helmets$ 90.00
Sheet1
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
What about this then?

VBA Code:
Sub AllocateCategories_v2()
  Dim rA As Range
  Dim oSet As Long
  
  Const HdrRow As Long = 1    '<- Header row
  Const PCol As String = "J"  '<- Price column
  Const RCol As String = "O"  '<- Result column
  
  oSet = Columns(PCol).Column - Columns(RCol).Column
  Application.ScreenUpdating = False
  With Range(RCol & HdrRow + 1).Resize(Range(PCol & Rows.Count).End(xlUp).Row - HdrRow)
    .NumberFormat = "000"
    For Each rA In .Offset(, oSet).SpecialCells(xlConstants).Areas
      rA.Offset(, -oSet).FormulaR1C1 = _
        Replace("=IF(COUNTIF(" & rA.Address(, , xlR1C1) & ",RC[#])=1,"""",IFNA(VLOOKUP(RC[#],R" & rA.Row - 1 & "C[#]:R[-1]C,-#+1,0),MAX(R1C:R[-1]C)+1))", "#", oSet)
    Next rA
    .Value = .Value
  End With
  Application.ScreenUpdating = True
End Sub

If the headers will already be in the sheet and in row 1 then instead of editing PCol and RCol in the code, the code could locate the headers itself.
Or if "Category Results" header is not there (but "Price" is) and results were to go in the next available column, that could also be done in the code.
 
Upvote 0
What about this then?

VBA Code:
Sub AllocateCategories_v2()
  Dim rA As Range
  Dim oSet As Long
 
  Const HdrRow As Long = 1    '<- Header row
  Const PCol As String = "J"  '<- Price column
  Const RCol As String = "O"  '<- Result column
 
  oSet = Columns(PCol).Column - Columns(RCol).Column
  Application.ScreenUpdating = False
  With Range(RCol & HdrRow + 1).Resize(Range(PCol & Rows.Count).End(xlUp).Row - HdrRow)
    .NumberFormat = "000"
    For Each rA In .Offset(, oSet).SpecialCells(xlConstants).Areas
      rA.Offset(, -oSet).FormulaR1C1 = _
        Replace("=IF(COUNTIF(" & rA.Address(, , xlR1C1) & ",RC[#])=1,"""",IFNA(VLOOKUP(RC[#],R" & rA.Row - 1 & "C[#]:R[-1]C,-#+1,0),MAX(R1C:R[-1]C)+1))", "#", oSet)
    Next rA
    .Value = .Value
  End With
  Application.ScreenUpdating = True
End Sub

If the headers will already be in the sheet and in row 1 then instead of editing PCol and RCol in the code, the code could locate the headers itself.
Or if "Category Results" header is not there (but "Price" is) and results were to go in the next available column, that could also be done in the code.
Thank you very much it works perfectly !!

thanks again for your time
 
Upvote 0
You're welcome. Thanks for the confirmation. :)
 
Upvote 0
You're welcome. Thanks for the confirmation. :)
Good morning
i was just wondering if i wanted to add an imput box for the header row or price column would it be as simpe as dim aa as string and aa= imput box... ?
 
Upvote 0
would it be as simpe as dim aa as string and aa= imput box... ?
Why not give it a try? ;)


if i wanted to add an imput box for the header row or price column
Why "or"? If we asked one how would we know the other?
Also, are you implying that the Category Results column is now fixed? If not, how would we know where that was?
 
Upvote 0
Why not give it a try? ;)



Why "or"? If we asked one how would we know the other?
Also, are you implying that the Category Results column is now fixed? If not, how would we know where that was?
Hi

sorry for not being clear i meant to add an input box question for the price column And another input box question for the header row

I would basically look at the sheet and see where those columns are and input them

I actually tried it and i got a "constant expression required"
 
Upvote 0
Try this

VBA Code:
Sub AllocateCategories_v3()
  Dim Bits As Variant
  Dim rA As Range
  Dim oSet As Long, HdrRow As Long
  Dim PCol As String, RCol As String, Resp As String
 
  Resp = InputBox("Enter header row, Price column & Result column separated by commas. eg 1,J,O")
  Bits = Split(Resp, ",")
  If UBound(Bits) = 2 Then
    If Evaluate("isref(" & Bits(1) & Bits(0) & ")") And Evaluate("isref(" & Bits(2) & Bits(0) & ")") Then
      HdrRow = Bits(0)
      PCol = Bits(1)
      RCol = Bits(2)
      oSet = Columns(PCol).Column - Columns(RCol).Column
      Application.ScreenUpdating = False
      With Range(RCol & HdrRow + 1).Resize(Range(PCol & Rows.Count).End(xlUp).Row - HdrRow)
        .NumberFormat = "000"
        For Each rA In .Offset(, oSet).SpecialCells(xlConstants).Areas
          rA.Offset(, -oSet).FormulaR1C1 = _
            Replace("=IF(COUNTIF(" & rA.Address(, , xlR1C1) & ",RC[#])=1,"""",IFNA(VLOOKUP(RC[#],R" & rA.Row - 1 & "C[#]:R[-1]C,-#+1,0),MAX(R1C:R[-1]C)+1))", "#", oSet)
        Next rA
        .Value = .Value
      End With
      Application.ScreenUpdating = True
    Else
      MsgBox "Incorrect input"
    End If
  Else
    MsgBox "Incorrect input"
  End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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