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
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Edit Macro as in post#3.
Insert this worksheet event code for each sheet.
Code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F3:O503")) Is Nothing Then
Application.EnableEvents = False
Run "PopulateColumn"
Application.EnableEvents = True
End If
End Sub
How to use worksheet event the code
Right click on Sheet tab --> view code
Visual Basic (VB) window opens.
Paste the code
Close the VB window.
Save the file as .xlsm
 
Upvote 0
No changes. Same loop is delayed the process.
You said the original ran fine. The only thing I added was an ELSE branch to the existing IF statement right before the NEXT r. This should not have changed the speed drastically. I will look for additional optimization.
 
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.
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.

Have you seen post#9
Yes, I have seen your post. Since my office laptop runs with company policy, I neither can attach XL2BB nor I can attach my original file to any cloud base storage. Sorry for that.
 
Upvote 0
You said the original ran fine. The only thing I added was an ELSE branch to the existing IF statement right before the NEXT r. This should not have changed the speed drastically. I will look for additional optimization.
After adding the ELSE branch with
Cells(r, "D").Value = ""
the cursor cannot move to the another cell until the loop fr to lr is completed. I want it to be used under Worksheet_SelectionChange() since column F to O is changing based on input sourced from other sheet.
 
Upvote 0
After adding the ELSE branch with

the cursor cannot move to the another cell until the loop fr to lr is completed. I want it to be used under Worksheet_SelectionChange() since column F to O is changing based on input sourced from other sheet.
The ELSE branch shouldn't cause this problem. It doesn't change the behavior in any way. It only changes what is written to the cell.
 
Upvote 0
The ELSE branch shouldn't cause this problem. It doesn't change the behavior in any way. It only changes what is written to the cell.
I am trying to attach a sample excel file using my home pc. Kindly allow me some time. Thank you and grateful for taking interest in my topic.
 
Upvote 0
Auto Serial.xlsm
DEFGHIJKLMNO
31. Swapnil 2. Raju 3. Saheb 4. Sayan 5. Monojit 6. Utsab 7. Priyank 8. Jaison 9. SriramSwapnilRajuSahebSayanMonojitUtsabPriyankJaisonSriram
4
5
61. Swapnil 2. Raju 3. Saheb 4. Monojit 5. Utsab 6. Jaison 7. SriramSwapnilRajuSahebMonojitUtsabJaisonSriram
7
81. Sayan 2. Rahul 3. Monojit 4. Utsab 5. Jaison 6. SriramSayanRahulMonojitUtsabJaisonSriram
9
10
11
121. Swapnil 2. Raju 3. Sayan 4. Rahul 5. Utsab 6. Priyank 7. SriramSwapnilRajuSayanRahulUtsabPriyankSriram
13
14
151. Swapnil 2. Sayan 3. Rahul 4. Monojit 5. Utsab 6. PriyankSwapnilSayanRahulMonojitUtsabPriyank
16
17
18
191. Swapnil 2. Raju 3. Saheb 4. Jaison 5. SriramSwapnilRajuSahebJaisonSriram
Sheet1
 
Upvote 0
Auto Serial.xlsm
DEFGHIJKLMNO
31. Swapnil 2. Raju 3. Saheb 4. Sayan 5. Monojit 6. Utsab 7. Priyank 8. Jaison 9. SriramSwapnilRajuSahebSayanMonojitUtsabPriyankJaisonSriram
4
5
61. Swapnil 2. Raju 3. Saheb 4. Monojit 5. Utsab 6. Jaison 7. SriramSwapnilRajuSahebMonojitUtsabJaisonSriram
7
81. Sayan 2. Rahul 3. Monojit 4. Utsab 5. Jaison 6. SriramSayanRahulMonojitUtsabJaisonSriram
9
10
11
121. Swapnil 2. Raju 3. Sayan 4. Rahul 5. Utsab 6. Priyank 7. SriramSwapnilRajuSayanRahulUtsabPriyankSriram
13
14
151. Swapnil 2. Sayan 3. Rahul 4. Monojit 5. Utsab 6. PriyankSwapnilSayanRahulMonojitUtsabPriyank
16
17
18
191. Swapnil 2. Raju 3. Saheb 4. Jaison 5. SriramSwapnilRajuSahebJaisonSriram
Sheet1
In this file your code is not taking that long time what I have experienced in my office file. May be that file is too big (almost 3 mb and other vba code is executing under Worksheet_Selection Change() Sub. Is there any other alternate to run the code smoothly without adding that ELSE Branch. Because that ELSE Branch is slowing down the process in my office file.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
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