Auto Serial Number in a Cell

arijitirf

Board Regular
Joined
Aug 11, 2016
Messages
110
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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Your code does not clear the data in column D. So if it is populated previously, it will not erase that data. If that is what you want it to do, try adding to the last If so it clears D:
VBA Code:
' 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
 
Upvote 0
Try this change
VBA Code:
If Len(str) > 0 Then
Cells(r, "D").Value = Left(str, Len(str) - 1) : str=""
End If
@NateSC
I tried with the changes you have mentioned, but after run the in worksheet selection change and after run the code it is falling under loop.

@kvsrinivasamurthy
Your code is also not working under Worksheet selection change sub.
 
Upvote 0
@NateSC
Since the code it too long to excute, is there any other alternative to get result faster. Next R is taking too long to respond under worksheet selection change sub.

Thanks in advance.
 
Upvote 0
Is there a reason you are running this code through the Worksheet_SelectionChange event? The solutions suggested will work fine if you add it as module level code.
 
Upvote 0
Is there a reason you are running this code through the Worksheet_SelectionChange event? The solutions suggested will work fine if you add it as module level code.
I want to run this code in Worksheet_Selection Change because data in Column F to O keep changing in through some excel formula based on input made in another sheet.
 
Upvote 0
If that's the case I think you need the Worksheet_Change event, which triggers whenever any cell in the worksheet is changed. The Worksheet_SelectionChange event triggers when a cell is selected.
In any event it can't be just a Sub - it needs to specifically be that event in the relevant sheet.

Following incorporates modification by @NateSC
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim MyRange 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
    Set MyRange = Range("F3:O503")
    If Not Intersect(Target, MyRange) Is Nothing Then
    
        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)
            Else
                Cells(r, "D").Value = ""
            End If
        Next r
        
        Application.ScreenUpdating = True
        
    End If
End Sub
 
Upvote 0
Can you post a sample file in some website and give link here.
Also I think F to O there are formulas and the output varies when changes are made in other cells.
 
Upvote 0
If that's the case I think you need the Worksheet_Change event, which triggers whenever any cell in the worksheet is changed. The Worksheet_SelectionChange event triggers when a cell is selected.
In any event it can't be just a Sub - it needs to specifically be that event in the relevant sheet.

Following incorporates modification by @NateSC
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim MyRange 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
    Set MyRange = Range("F3:O503")
    If Not Intersect(Target, MyRange) Is Nothing Then
  
        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)
            Else
                Cells(r, "D").Value = ""
            End If
        Next r
      
        Application.ScreenUpdating = True
      
    End If
End Sub
No changes. Same loop is delayed the process.
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,717
Members
449,465
Latest member
TAKLAM

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