Auto Serial Number in a Cell

arijitirf

Board Regular
Joined
Aug 11, 2016
Messages
118
Office Version
  1. 2016
Platform
  1. Windows
Hi!
I have been using a code below, that is working flawlessly. I want data in D to be updated if I remove data from Column F to O.

Suppose, there is data in F3, G3, J3, K3, N3 & O3 and after execute the code data will populate in D3. Now if I delete all the data from F3 and O3 and run the code then D3 will not populate anything since there is no data b/w Column F and O (in the below code, data in D3 is still showing after run the code though there is no data b/w Column F and O)

Sub PopulateColumn()

Dim fr as Long
Dim lr As Long
Dim r As Long
Dim c As Long
Dim ct As Long
Dim str As String

Application.ScreenUpdating = False

' Set first and last rows to loop through
fr = 3
lr = 503

' Loop through all rows
For r = fr To lr
' Reset counter and string variable
ct = 0
str = ""
' Loop through columns F (6) to O (15)
For c = 6 To 15
' Check to see if it is a non-blank value
If Cells(r, c).Value <> "" Then
' Add one to counter
ct = ct + 1
' Build on to string
str = str & ct & ". " & Cells(r, c).Value & Chr(10)
End If
Next c
' Put result in column D
If Len(str) > 0 Then
Cells(r, "D").Value = Left(str, Len(str) - 1)
End If
Next r

Application.ScreenUpdating = True

End Sub

Requesting help.

Thanks in advance
 
An improvement would be to only run this for the changed row. Don't process all rows. try this:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim fr As Long
Dim lr As Long
Dim r As Long
Dim c As Long
Dim ct As Long
Dim str As String

    r = Target.Row
    'Application.ScreenUpdating = False

    ' Reset counter and string variable
    ct = 0
    str = ""
 
    For c = 6 To 15
        ' Check to see if it is a non-blank value
        If Cells(r, c).Value <> "" Then
        ' Add one to counter
        ct = ct + 1
        ' Build on to string
        str = str & ct & ". " & Cells(r, c).Value & Chr(10)
        End If
    Next c
    ' Put result in column D
    If Len(str) > 0 Then
        Cells(r, "D").Value = Left(str, Len(str) - 1)
    Else
        Cells(r, "D").Value = ""
    End If

    'Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
An improvement would be to only run this for the changed row. Don't process all rows. try this:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim fr As Long
Dim lr As Long
Dim r As Long
Dim c As Long
Dim ct As Long
Dim str As String

    r = Target.Row
    'Application.ScreenUpdating = False

    ' Reset counter and string variable
    ct = 0
    str = ""
 
    For c = 6 To 15
        ' Check to see if it is a non-blank value
        If Cells(r, c).Value <> "" Then
        ' Add one to counter
        ct = ct + 1
        ' Build on to string
        str = str & ct & ". " & Cells(r, c).Value & Chr(10)
        End If
    Next c
    ' Put result in column D
    If Len(str) > 0 Then
        Cells(r, "D").Value = Left(str, Len(str) - 1)
    Else
        Cells(r, "D").Value = ""
    End If

    'Application.ScreenUpdating = True

End Sub
Excellent! Now it is working without any flaw. Thank you so much @NateSC and others who have taken interest in my topic and help me to find solutions.
 
Upvote 1
I think you could improve the speed further if needed by reading the values to an array and then performing the FOR loop on the array. I tried this and ran into a problem I couldn't solve quickly.
 
Upvote 0
I think you could improve the speed further if needed by reading the values to an array and then performing the FOR loop on the array. I tried this and ran into a problem I couldn't solve quickly.
Ok. But since the code is running perfectly fine with the modified version of the code you provided, it is not required. I must appreciate your effort. Thanks a Ton @NateSC
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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