Adding a serial number into a worksheet sheet1 with VBA

59 Views Asked by At

I have a sheet1 with Like this:

     ColumnA     ColumnB
1    SerialNo.   Smartphones
2                iphone 15
3                iphone 15 pro
4                iphone 15 plus
5                iphone 15 pro max


So I came up with this code that puts a rows count into ColumnA whenever I put a smartphone name in ColumnB

Private Sub 
 Worksheet_SelectionChange(ByVal Target As Range)

Dim count As Long

count=Application.WorksheetFunction.CountA(Sheet1.Range("B:B")

For i = 1 to count
Sheet1.Range("A" & i + 1).Value = i
Next i
End sub

But here it does only if I enter something in columnB ,

Is it possible if I enter something in like any column of this selected sheet ,that put a rowcounts in the ColumnA
?


 


I tried various functions related to the serial number from this site and from others too , but didn't found anything which can put a serial no. If u write something in the smartphones column.

Can anyone know it how to put a serial number automatically, with the VBA code?

0

There are 0 best solutions below