VBA renumbering rows after deleting a row.

James824

New Member
Joined
Oct 14, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi, so I also have a macro that if activated will delete the selected row and another that will add in a row at the bottom. The row has a heading that is numbered from 1 through to the number of rows. Currently, if a row is deleted half way within the table (for example), the numbering is messed up. If I then go to add in another row at the bottom, it tries to number the new row with a number that is already taken and an error pops up. Is there any way of implementing something that renumbers all the remaining rows after deleting one row, which I believe would solve this problem. Any help with this would be greatly appreciated.

Many thanks,
J
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You can define an array and use the "for" function to put new numberings loop into it. Then written the array to the column where numberings is located.
 
Upvote 0
Another way is to use a formula for your row numbers ...

Book1
MNOP
270col1col2col3
281
292
303
314
325
336
347
358
Sheet1
Cell Formulas
RangeFormula
M28:M35M28=M27+1
 
Upvote 0
That's great! Many thanks for you advice. The code seems to work the first time that I use the buttons to complete the previously mentioned actions, however, after any other time, the following message keeps popping up. "Run-time error '1004': Table features aren't available because the sheet is protected". I believe that the sheets should be unprotected when it runs into this as I have the following in place at the start of the sub.
ActiveSheet.Unprotect Password:="password"

Could there be something that I am missing with this?

Many thanks,
J
 
Upvote 0
Strictly speaking, the ActiveSheet can be any worksheet. To be sure make a proper reference to the worksheet you're addressing.
For example, provided the code is in the same workbook as that particular worksheet :

VBA Code:
ThisWorkbook.Sheets("SheetWithTable").Unprotect Password:="password"
 
Upvote 0

Forum statistics

Threads
1,214,815
Messages
6,121,715
Members
449,049
Latest member
THMarana

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