Assign sequential number to the cell with condition

billmark

Board Regular
Joined
Dec 11, 2011
Messages
59
Hi
I have the following 2 columns. I want to assign the order no. for each client using VBA as below. There are several worksheets in the workbook. But the code doesn't work. Please help.
[TABLE="width: 201"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD][TABLE="width: 201"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]Order[/TD]
[TD]Client[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]123[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD="align: right"]123[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]4[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]5[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]6[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]7[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]8[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]9[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]10[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]11[/TD]
[TD="align: right"]549465[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]8789465[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD="align: right"]8789465[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD="align: right"]8789465[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

QUOTE
Sub assignnumber()



Dim lr As Long
Dim WS As Worksheet
Dim x As Long




For Each WS In Worksheets
lr = WS.Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lr
WS.Range("A2") = 1
If WS.Range("B" & x) = WS.Range("B" & x - 1) Then
WS.Range("A" & x).Value = WS.Range("A" & x - 1) + 1

End If
Next x
Next WS


End Sub
UNQUOTE

Regards
Mark
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
If you wanted to keep most of that code, you would just need to make these two changes

Rich (BB code):
Sub assignnumber()
  Dim lr As Long
  Dim WS As Worksheet
  Dim x As Long
  
  For Each WS In Worksheets
    lr = WS.Cells(Rows.Count, 2).End(xlUp).Row
    For x = 2 To lr
      WS.Range("A" & x) = 1
      If WS.Range("B" & x) = WS.Range("B" & x - 1) Then
        WS.Range("A" & x).Value = WS.Range("A" & x - 1) + 1
      End If
    Next x
  Next WS
End Sub

BTW, ..
1. My signature block below shows you how to post code & keep its layout formatting.
2. Just saying something doesn't work does not give helpers much to go on. Did it error, give right results in wrong place, give wrong results in right place, do nothing etc?
 
Last edited:
Upvote 0
I believe this macro will also work...
Code:
Sub AssignNumbers()
  With Range("B2", Cells(Rows.Count, "B").End(xlUp)).Offset(, -1)
    .Formula = "=IF(B1<>B2,1,COUNTIF(B$1:B2,B2))"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Thanks for both Peter and Rick

Hi Rick
How the code to apply to multiple worksheets in a workbook.
 
Upvote 0
How the code to apply to multiple worksheets in a workbook.
Looks like your column B data is sorted so try
Code:
Sub assignnumber_v2()
  Dim WS As Worksheet
  
  For Each WS In Worksheets
    With WS.Range("A2:A" & WS.Cells(WS.Rows.Count, 2).End(xlUp).Row)
      .Formula = "=IF(B1=B2,A1+1,1)"
      .Value = .Value
    End With
  Next WS
End Sub
 
Last edited:
Upvote 0
Looks like your column B data is sorted so try
Code:
Sub assignnumber_v2()
  Dim WS As Worksheet
  
  For Each WS In Worksheets
    With WS.Range("A2:A" & WS.Cells(WS.Rows.Count, 2).End(xlUp).Row)
      .Formula = "=IF(B1=B2,A1+1,1)"
      .Value = .Value
    End With
  Next WS
End Sub


Thanks.

I have question, why this statement used 2 but not 1

WS.Cells(WS.Rows.Count, 2
 
Upvote 0
I have question, why this statement used 2 but not 1

WS.Cells(WS.Rows.Count, 2
2 means the code uses column B to determine how many rows of data you have in the sheet. Using 1 would mean the code used column A to work that out. If column A is empty (waiting to have the order numbers inserted) then the code will not process any rows. If column A is empty, that was one of the reasons that the code you originally posted did not work for you.
 
Upvote 0
2 means the code uses column B to determine how many rows of data you have in the sheet. Using 1 would mean the code used column A to work that out. If column A is empty (waiting to have the order numbers inserted) then the code will not process any rows. If column A is empty, that was one of the reasons that the code you originally posted did not work for you.


Thanks
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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