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