Macro to assign consecutive numbers to non-consecutive cells

meadscarla

New Member
Joined
Apr 3, 2013
Messages
7
I am trying to assign a unique, consecutive value to cells in column D based on the value in the corresponding cell in column A. Wherever the cell in Column A equals "New", a value should be populated into the cell in column D.
This value should be a user defined code of letters followed by 0001, 0002, 0003 etc.

i.e. I am trying to achieve this:

AD
NewABC0001
Old
Changed
Old
NewABC0002
NewABC0003
Old
NewABC0004

<tbody>
</tbody>

The user defined code of letters is input via an input box. Here is what I have so far.

Code:
InterimCode = InputBox("Please enter code for Interim Number")
Range("D1").Select
Do
ActiveCell.Offset(1, 0).Range("A1").Select
If ActiveCell.Offset(0, -3).Range("A1") = "New" Then
ActiveCell.FormulaR1C1 = InterimCode & "0001"
Else
ActiveCell.Offset(1, 0).Range("A1").Select
End If
Loop Until IsEmpty(ActiveCell.Offset(0, -3))

Obviously this just places ABC0001 in each cell in column D so I am looking for how I can ensure each value is the next consecutive number.

Thanks.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This is how I would approach it (unless you have many thousands of rows)
Code:
Sub AssignCodes()
  Dim InterimCode As String
  Dim n As Long
  Dim c As Range
  
  InterimCode = InputBox("Please enter code for Interim Number")
  For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp))
    If c.Value = "New" Then
      n = n + 1
      c.Offset(, 3).Value = InterimCode & Format(n, "0000")
    End If
  Next c
End Sub
 
Upvote 0
You could probably achieve this with a worksheet function.


Excel 2007
ABC
1ADABC
2NewABC0001ABC0001
3Old
4Changed
5Old
6NewABC0002ABC0002
7NewABC0003ABC0003
8Old
9NewABC0004ABC0004
Sheet1
Cell Formulas
RangeFormula
C2=IF(A2<>"New","",$C$1&TEXT(COUNTIF($A$2:A2,"New"),"0000"))
 
Upvote 0
Why do you need a macro to do this?

You can use a formula?
Excel Workbook
ABC
1NewABC0001ABC0001
2Old
3Changed
4Old
5NewABC0002ABC0002
6NewABC0003ABC0003
7Old
8NewABC0004ABC0004
9NewABC0005
10NewABC0006
11NewABC0007
12NewABC0008
13NewABC0009
14NewABC0010
15NewABC0011
16NewABC0012
Sheet1
Cell Formulas
RangeFormula
C1=IF(A1="New","ABC"&TEXT(COUNTIF($A$1:A1,A1),"000#"),"")

You could also replace ABC with a cell reference if you want. Or this can be converted into a macro if you really need a macro.
 
Upvote 0
Thanks all.
Peter your code worked perfectly.

It needed a macro as there are a whole host of other functions taking place on a large amount of data coming from multiple sources.
This was just a very small part of it that I couldn't figure out.
 
Upvote 0

Forum statistics

Threads
1,203,051
Messages
6,053,221
Members
444,648
Latest member
sinkuan85

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