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
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