Excel VBA Help

M_S

New Member
Joined
Aug 6, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

I need some help with the data below. So, I'm trying to assign numbers to the Values in Column B. For example ,if Column B has Apple in it and I assign it 22, I want the next item on the list to be 23 and so on. Same items should have same value, as you can see in Column A.

How can I achieve this using VBA? Any help would be much appreciated! Thanks!

Column AColumn B
22Apple
22Apple
22Apple
23Banana
23Banana
22Apple
23Banana
24Orange
24Orange
23Banana
22Apple
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Does it need to be in VBA - you can accomplish the same goal with a formula - as below...

Book1
AB
1Column AColumn B
222apple
322apple
422apple
523banana
623banana
722apple
823banana
924orange
1024orange
1123banana
1222apple
1325watermelon
1422apple
1523banana
1623banana
1725watermelon
1826strawberry
1927raspberries
2025watermelon
2126strawberry
2222apple
2322apple
2423orange
2524orange
Sheet1
Cell Formulas
RangeFormula
A3:A25A3=IF(COUNTIF($B$2:$B2,B3)>0,INDEX($A$2:$B2,MATCH(B3,$B$2:$B2),1),MAX($A$2:$A2)+1)


It may not be a particularly elegant formula, and it requires a starting number for the first item (see A2 = 22), but other than that it seems to work.
 
Upvote 0
Does it need to be in VBA - you can accomplish the same goal with a formula - as below...

Book1
AB
1Column AColumn B
222apple
322apple
422apple
523banana
623banana
722apple
823banana
924orange
1024orange
1123banana
1222apple
1325watermelon
1422apple
1523banana
1623banana
1725watermelon
1826strawberry
1927raspberries
2025watermelon
2126strawberry
2222apple
2322apple
2423orange
2524orange
Sheet1
Cell Formulas
RangeFormula
A3:A25A3=IF(COUNTIF($B$2:$B2,B3)>0,INDEX($A$2:$B2,MATCH(B3,$B$2:$B2),1),MAX($A$2:$A2)+1)


It may not be a particularly elegant formula, and it requires a starting number for the first item (see A2 = 22), but other than that it seems to work.
Thank you, Dan. Yes, I was actually looking for something in VBA.

Thanks for you help tho, appreciate it!
 
Upvote 0
Curious - what do you need it for? I may be able to help if you can give some more information.
 
Upvote 0
How about
VBA Code:
Sub M_S()
   Dim Cl As Range
   Dim StartNo As Variant
   
   StartNo = InputBox("Please enter a start number")
   If StartNo = "" Then Exit Sub
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then .Add Cl.Value, StartNo + .Count
         Cl.Offset(, -1).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub M_S()
   Dim Cl As Range
   Dim StartNo As Variant
  
   StartNo = InputBox("Please enter a start number")
   If StartNo = "" Then Exit Sub
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then .Add Cl.Value, StartNo + .Count
         Cl.Offset(, -1).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
Thank you so much! It worked like Magic, you're awesome!
Is it possible, if there's another column "C" and I want to match if it falls in the same category. So, everything in Bucket 1 will have a series and then Bucket 2 will have continuation of the series, regardless if column B has same value?


Column AColumn BColumn C
1AppleBucket 1
1AppleBucket 1
2BananaBucket 1
3OrangeBucket 1
4OrangeBucket 2
5AppleBucket 2
5AppleBucket 2
6BananaBucket 3
7AppleBucket 3
8OrangeBucket 4
8OrangeBucket 4
9AppleBucket 4
10BananaBucket 4
 
Upvote 0
How about
VBA Code:
Sub M_S()
   Dim Cl As Range
   Dim StartNo As Variant
   
   StartNo = InputBox("Please enter a start number")
   If StartNo = "" Then Exit Sub
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value & "|" & Cl.Offset(, 1).Value) Then .Add Cl.Value & "|" & Cl.Offset(, 1).Value, StartNo + .Count
         Cl.Offset(, -1).Value = .Item(Cl.Value & "|" & Cl.Offset(, 1).Value)
      Next Cl
   End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub M_S()
   Dim Cl As Range
   Dim StartNo As Variant
  
   StartNo = InputBox("Please enter a start number")
   If StartNo = "" Then Exit Sub
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value & "|" & Cl.Offset(, 1).Value) Then .Add Cl.Value & "|" & Cl.Offset(, 1).Value, StartNo + .Count
         Cl.Offset(, -1).Value = .Item(Cl.Value & "|" & Cl.Offset(, 1).Value)
      Next Cl
   End With
End Sub
This didn't work. Does it have anything to do with "|"?
 
Upvote 0
It actually did work! Thanks a heap! I'm out of words.
Is there a way to apply this formula to only visible cells? I have a filter on in another column but for some reason it gets applied to hidden cells as well.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

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