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.
OrderClient
1123
2123
1549465
2549465
3549465
4549465
5549465
6549465
7549465
8549465
9549465
10549465
11549465
18789465
28789465
38789465

<colgroup><col><col></colgroup><tbody>
</tbody>

<colgroup><col><col></colgroup><tbody>
</tbody>

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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
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,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

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