Increment a Column

dr223

New Member
Joined
Oct 26, 2009
Messages
18
Hi,

I have the following spreadsheet, which has one column B and I want to add column C whereby if the fileds is the same in column B to have a unique value, if not go to the next field and auto increment the number...

The C column should be appearing as shown, any help please

B C

mango 1
mango 1
mango 1
banana 2
banana 2
banana 2
banana 2
banana 2
orange 3
grape 4
grape 4



Many thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I have done that but the value 1 is displayed in all the fields in Column C it is not changing to 2 when mango changes to banana

Any help please!
 
Upvote 0
It works for me

Excel Workbook
BC
1mango1
2mango1
3mango1
4banana2
5banana2
6banana2
7banana2
8banana2
9orange3
10grape4
11grape4
Sheet2
 
Upvote 0
Essentially the same as Peter's suggestion, but paste this code into the Sheet Module, then
just Double-Click anywhere in Column B for it to Run...

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
If Target.Column <> 2 Then
Exit Sub
Else
Lr = Range("B" & Rows.Count).End(xlUp).Row
Target.Offset(, 1).Select
End If
ActiveCell.EntireColumn.Insert (xlToRight)
Range("C1").Value = 1
Range("C2:C" & Lr).Formula = "=IF(B2<>B1,C1+1,C1+0)"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,421
Members
452,913
Latest member
JWD210

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