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
 
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
Sorry for late response this is perfect
thanks again
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You're welcome. Would that mean you change the 'Mark as solution' post?
 
Upvote 0
Sorry for late response this is perfect
thanks again
hi
i was thinking what happens if the item number is duplicated although rare in my case but it could happen, the solution would be in my case to just put the categories on whichever price is the lowest
 
Upvote 0
Sounds like quite a different question so I would start a new thread, link to this one if you want, and most importantly provide some varied sample data and expected results in a form that helpers can copy/paste so they are not faced with typing it all out manually.
 
Upvote 0

Forum statistics

Threads
1,214,877
Messages
6,122,051
Members
449,064
Latest member
scottdog129

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