Calculate columns automatically based on the first column

richardwm

New Member
Joined
Apr 22, 2016
Messages
6
Hi Dear All,

I have an excel sheet that is feeded by an Access table, so the number of the rows is variable.

I am looking for a code that calculate IDs based on the first ID (123 in this example).

So, the idea is to replace 1000 with (123 + 1) and 2000 with (123 + 1 +1) .. to the last column that is not empty.

The first ID is always in A2 Range.


IDName
123TEST1
1000TEST2
2000TEST3
3000TEST4
4000TEST5

<tbody>
</tbody>

Thank you in advance :)
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi Dear All,

I have an excel sheet that is feeded by an Access table, so the number of the rows is variable.

I am looking for a code that calculate IDs based on the first ID (123 in this example).

So, the idea is to replace 1000 with (123 + 1) and 2000 with (123 + 1 +1) .. to the last column that is not empty.

The first ID is always in A2 Range.


IDName
123TEST1
1000TEST2
2000TEST3
3000TEST4
4000TEST5

<tbody>
</tbody>

Thank you in advance :)
Hi Richard, welcome to the boards.

How about something like this?

Code:
Sub UpdateColumn()
' Defines variables
Dim LastRow As Long, Rng As Range


' Defines LastRow as the last row of data based on column A
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row


' Sets Rng As A3 to the last row of A
Set Rng = Range("A3:A" & LastRow)
' Updates cell A3 with a formula A2+1
Range("A3").Formula = "=A2+1"
' Copy A3
Range("A3").Copy
' Paste from A3 to the last row of A
Rng.PasteSpecial xlPasteAll
' Copy A3 to the last row of A
Rng.Copy
' Paste as values
Rng.PasteSpecial xlPasteValues


End Sub
 
Upvote 0
Here's simpler, more direct, way for you to consider too.

Code:
Sub IncrementIDs()
  Range("A2", Range("A" & Rows.Count).End(xlUp)).DataSeries Type:=xlLinear, Step:=1
End Sub
 
Upvote 0
Here's simpler, more direct, way for you to consider too.

Code:
Sub IncrementIDs()
  Range("A2", Range("A" & Rows.Count).End(xlUp)).DataSeries Type:=xlLinear, Step:=1
End Sub
Genius! I didn't even realise that was a thing!
 
Upvote 0

Forum statistics

Threads
1,216,040
Messages
6,128,454
Members
449,455
Latest member
jesski

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