View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Auric__ Auric__ is offline
external usenet poster
 
Posts: 538
Default record counter based on column value

Sabosis wrote:

This should be an easy one...

In column A I want to have a counter or series that only changes to the
next value when the value in a separate column changes.

If column B is the order number, I want cell A2 to begin with a value of
1 and only move to a value of 2 when the order number changes. it would
look like this

Counter Order # Product #
1 12345 65478
1 12345 52145
1 12345 65214
2 65252 98547
2 65252 65298
3 98765 96745
3 98765 63756
3 98765 12856


Note that if the orders aren't continguous -- say, 123 followed by 456 and
then back to 123, then this won't be accurate. (That situation would require
keeping track of all known orders and their assigned counter numbers. Not
hard, just monkey work. Probably take another 3-5 lines of code.)

Note also that this assumes that the order # is column B, and the counter is
column A.

Sub record_counter_based_on_column_value()
'assumes row 1 is header
Dim ctr, L0

'counter's start value; adjust as appropriate
Cells(2, 1).Value = 1

For L0 = 3 To Cells.SpecialCells(xlCellTypeLastCell).Row
If Cells(L0, 2).Value < Cells(L0 - 1, 2).Value Then
Cells(L0, 1).Value = Cells(L0 - 1, 1).Value + 1
Else
Cells(L0, 1).FillDown
End If
Next
End Sub

--
He straightened, assuming an odd attitude of dignity --
as though it were another mask, but this time clothing his entire body.