VBA : Count sequentially in a table column, all the way down to the end of the table

jase71ds

Board Regular
Joined
Oct 23, 2006
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Thanks for any help on this - I appreciate the gift of time and knowledge that so many on this forum freely give :)

I have an Excel Table named "tbl_Schedule"
The first column is named "Counting"

The Counting column's purpose to start with 1 and count sequentially all the way down the table to the very last row in the table.

The table has nearly 2,000 rows and each week I'm having to insert new rows within the middle of the table.
Each time I do this I have to re-number.

I'd like to have a macro that, upon execution, starts with 1 in the first row after the column header: "Counting", and counts sequentially down to the last row of tbl_Schedule.
Keep in mind, because I will run this macro after having just inserted new rows somewhere within the table, I can't use the VBA trick of looping until it finds the first blank cell (because the new rows will have blank cells). I need the macro to look at the true range of the Excel Table and know how far down to count.

Hope that was clear.
As stated earlier - any help is very much appreciated.

Jase.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Does this do what you want...

Code:
Sub InsertRowNum()


    Dim tbl As ListObject
    Dim x As Long


    Set tbl = ActiveSheet.ListObjects("tbl_Schedule")
    For x = 1 To tbl.ListRows.Count
        tbl.DataBodyRange(x, 1) = x
    Next x
    
End Sub
 
Upvote 0
Thank you very much. That seems to work perfectly :D
Much appreciated!!
 
Upvote 0
Great, you're welcome. Thanks for the feedback!
 
Upvote 0
igold - I have a follow-up question.
The code populates the first column of the table - which is exactly what I needed - but I'm wondering for future modification, how might I modify this code if I ever wanted it to do the same action, but on say the 4th column of the table, named "ActivityNumber"?
Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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