Auto serial number

NDMDRB

Board Regular
Joined
Jun 20, 2016
Messages
164
Office Version
  1. 2016
Platform
  1. Windows
Hello,

Please I need your help, if this is any possible way in Excel

I have a userform to show data on a list box, add new data to the sheet, and remove any selected row from the list box and the sheet
I have all these buttons and codes working well, and I have uploaded my test file below

At the moment, if (for example) I removed "Row7", the sort code will run and "Row8" data will be in "Row7" and will have the same serial number "4"

What I'm looking for is:
  • ex: If I removed "Row 7" that has a serial number "3", then...
  • Run the sort code, then...
  • "Row8" data will be in "Row7" but with serial number "3"

In conclusion, I need column "B" to always has a serial number

here is my test file
https://drive.google.com/file/d/1QsoxAwmqRgCkXJkpYtTOebbDacwRVp3W/view?usp=sharing
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
@Bering
Can you please post your suggestion to the board, so that everyone can see it without having to download a workbook.
 
Upvote 0
One way would be to use a formula for the Num's in column "B"
=Row() - 4
and when adding write the formula instead of an actual number to column B
 
Upvote 0
@Bering
Can you please post your suggestion to the board, so that everyone can see it without having to download a workbook.

Sorry Fluff:

Module1

original code
Code:
Sub SortList()With Sheet1
.Range("B4:G9999").Sort key1:=.Range("B4"), order1:=xlAscending, Header:=xlNo
End With
End Sub

my attempt
Code:
Sub SortList()Const IncrementalFactor As Integer = 1
Const FirstRow As Integer = 6


With Sheet1
.Range("B4:G9999").Sort key1:=.Range("B4"), order1:=xlAscending, Header:=xlNo


If .Cells(5, 3) <> "" Then


.Cells(5, 2) = IncrementalFactor


Else


.Cells(5, 2) = ""


End If


    For i = FirstRow To .Cells(.Rows.Count, 3).End(xlUp).Row


        .Cells(i, 2) = .Cells(i - 1, 2) + 1
    
    Next i
    
End With
End Sub

I have also added a On Error Resume Next in the UserForm1 macro to prevent any error if all rows get deleted:
Code:
Private Sub cmdRemove_Click()
On Error Resume Next

The issue is in the row below:
Code:
ListBox1.RowSource = Sheet1.Range("Items").Address(external:=True)
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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