Copy/Paste and AutoNumbering

FalconFlyer

New Member
Joined
May 18, 2015
Messages
30
I am copy some data from one worksheet into a master worksheet (same workbook). The master worksheet has a unique ID (next number). After I copy the data over the master worksheet, I would like to fill in the ID. What I need the macro to do is fill in the missing ID numbers to the end of the data. My ID is in Column A. The end of the data should be based off of Column B. For example, I need to fill in ID 1003 - 1005 for the corresponding data in Column B. The code I have been trying is below.

Column A Column B
1001 abc
1002 def
ghi
jkl
mno

Code:
Sub AutoNum()

Dim LastRow As Long
Dim FirstRow As Long
Dim Start As Long
Dim NextNumber As Long

LastRow = Range("B" & Rows.Count).End(xlUp).Row
FirstRow = Range("A" & Rows.Count).End(xlUp).Row + 1
NextNumber = WorksheetFunction.Max(Sheet1.Range("A:A")) + 1

    For counter = FirstRow To LastRow
    Range("A" & counter).Value = NextNumber
Next counter
End Sub

With this code, I keep getting "1" as the next ID number. I do not understand what I am doing wrong. I have tried the MAX function by itself and it is still giving me "1", but when I write it in Excel as =MAX(A:A) +1, I am getting the expected answer.

Thank you in advance.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
This works for me:

Replace:
Code:
Range("A" & counter).Value = NextNumber
With:
Code:
      If Range("A" & counter).Value = vbNullString Then _
        Range("A" & counter).Value = Range("A" & counter - 1).Value + 1

This test to see that the AutoNumber is blank and if it is, fill the blank with the value above plus one. If there are gaps (you've got three blank spaces (1003-1005) but the next autonumber should be 1004), it's obviously going to fail.

hth
 
Upvote 0
You're welcome. Thanks for the feedback and welcome to the board!!
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

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