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.


[TABLE="width: 500, align: center"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Name[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]TEST1[/TD]
[/TR]
[TR]
[TD]1000[/TD]
[TD]TEST2[/TD]
[/TR]
[TR]
[TD]2000[/TD]
[TD]TEST3[/TD]
[/TR]
[TR]
[TD]3000[/TD]
[TD]TEST4[/TD]
[/TR]
[TR]
[TD]4000[/TD]
[TD]TEST5[/TD]
[/TR]
</tbody>[/TABLE]

Thank you in advance :)
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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.


[TABLE="width: 500, align: center"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Name[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]TEST1[/TD]
[/TR]
[TR]
[TD]1000[/TD]
[TD]TEST2[/TD]
[/TR]
[TR]
[TD]2000[/TD]
[TD]TEST3[/TD]
[/TR]
[TR]
[TD]3000[/TD]
[TD]TEST4[/TD]
[/TR]
[TR]
[TD]4000[/TD]
[TD]TEST5[/TD]
[/TR]
</tbody>[/TABLE]

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,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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