Automatic Serial Number before Text in a Cell

arijitirf

Board Regular
Joined
Aug 11, 2016
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
Hi!
In Column D, data of Column F:O flows through Text Join skipping Blank cell. I want a formula that generates auto incremental serial number (1, 2, 3, 4 and so on).

Let's assume,

Data in Column
F as "Rameshwaram"
G as "Somenath"
H as "Kedarnath"
I as " "
J as "Baidyanath"
K as " "
L as " "
M as "Kashi Viswanath"
N as "Trimbakeshwar"
O as "Mahakaleshwar"

Then data in Column D will be
1. '"Rameshwaram"'
2. "Somenath"
3. "Kedarnath"
4. "Baidyanath"
5. "Kashi Viswanath"
6. "Trimbakeshwar"
7. "Mahakaleshwar"

Is there any way to do that. I have come across a excellent formula in this forum which can do the same for Row however, the same is not useful for me.

Thanks in advance
 
You mean everything in cell D1? like this?

Book1
BCDEFGHIJKLMNOPQ
11. Rameshwaram 2. Somenath 3. Kedarnath 4. Baidyanath 5. Kashi Viswanath 6. Trimbakeshwar 7. MahakaleshwarRameshwaramSomenathKedarnathBaidyanathKashi ViswanathTrimbakeshwarMahakaleshwar
2
3
4
Sheet2
Yes with Char(10) in between
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
A formula approach is probably beyond my abilities, but here is a VBA approach that will produce what the image Cubist shows (in column D):
VBA Code:
Sub PopulateColumn()

    Dim rng As Range
    Dim cell As Range
    Dim ct As Long
   
    Application.ScreenUpdating = False
   
'   Set range to loop through
    Set rng = Range("F1:O1")
   
'   Loop through all cells in rng
    For Each cell In rng
'       Check to see if it is a non-blank value
        If cell.Value <> "" Then
'           Add one to counter
            ct = ct + 1
'           Populate column D
            Cells(ct, "D").Value = ct & ". " & cell.Value
        End If
    Next cell
   
    Application.ScreenUpdating = True

End Sub

Result:
1712257967367.png
 
Upvote 0
@Joe4

With further clarification, the OP wants a concatenated string in one singular cell D1, separated by CHAR(10).
 
Upvote 0
A formula approach is probably beyond my abilities, but here is a VBA approach that will produce what the image Cubist shows (in column D):
VBA Code:
Sub PopulateColumn()

    Dim rng As Range
    Dim cell As Range
    Dim ct As Long
  
    Application.ScreenUpdating = False
  
'   Set range to loop through
    Set rng = Range("F1:O1")
  
'   Loop through all cells in rng
    For Each cell In rng
'       Check to see if it is a non-blank value
        If cell.Value <> "" Then
'           Add one to counter
            ct = ct + 1
'           Populate column D
            Cells(ct, "D").Value = ct & ". " & cell.Value
        End If
    Next cell
  
    Application.ScreenUpdating = True

End Sub

Result:
View attachment 109484
Thank you for your valuable code. Only change is the data of Column F1:O1 will flow in D1 and will continue to flow in the same row in Column D:D. It was my mistake that I forgot to mention the same in my first thread.
 
Upvote 0
@Joe4

With further clarification, the OP wants a concatenated string in one singular cell D1, separated by CHAR(10).
Precisely why pictures are so much better at explaining these things to show exactly what the desired result is!

I think this variation should do the trick:
VBA Code:
Sub PopulateColumn()

    Dim rng As Range
    Dim cell As Range
    Dim ct As Long
    Dim str As String
  
    Application.ScreenUpdating = False
  
'   Set range to loop through
    Set rng = Range("F1:O1")
  
'   Loop through all cells in rng
    For Each cell In rng
'       Check to see if it is a non-blank value
        If cell.Value <> "" Then
'           Add one to counter
            ct = ct + 1
'           Build on to string
            str = str & ct & ". " & cell.Value & Chr(10)
        End If
    Next cell
  
'   Put result in cell D1
    Range("D1") = Left(str, Len(str) - 1)
  
    Application.ScreenUpdating = True

End Sub

Result:
1712258632154.png
 
Upvote 0
Thank you both @Joe4
and @Cubist
for your help. Since I do not have my Laptop with me, I can not check it. I shall definitely let you know tomorrow. Thank you once again.
 
Upvote 0
Precisely why pictures are so much better at explaining these things to show exactly what the desired result is!

I think this variation should do the trick:
VBA Code:
Sub PopulateColumn()

    Dim rng As Range
    Dim cell As Range
    Dim ct As Long
    Dim str As String
 
    Application.ScreenUpdating = False
 
'   Set range to loop through
    Set rng = Range("F1:O1")
 
'   Loop through all cells in rng
    For Each cell In rng
'       Check to see if it is a non-blank value
        If cell.Value <> "" Then
'           Add one to counter
            ct = ct + 1
'           Build on to string
            str = str & ct & ". " & cell.Value & Chr(10)
        End If
    Next cell
 
'   Put result in cell D1
    Range("D1") = Left(str, Len(str) - 1)
 
    Application.ScreenUpdating = True

End Sub

Result:
View attachment 109485
Good morning from India.

Your code is perfectly working for Range F1:O1 and data from Range F1:O1 is perfectly flowing in D1.

I want the same result for each Row in Column D till D503. Like D2 will show the result from F2:O2 and so on till Range F503:O503.

Kindly check if you could help me with that. Thank you once again for spending your valuable time for me.
 
Upvote 0
Maybe this. The code assumes the activesheet.
VBA Code:
Sub PopulateColumn()
    Dim rng As Range
    Dim dataArr As Variant
    Dim outputArr() As String
    Dim i As Long, j As Long
    Dim ct As Long
  
    Application.ScreenUpdating = False
  
    Set rng = Range("F1:O3")
  
    dataArr = rng.Value
  
    ReDim outputArr(1 To UBound(dataArr, 1))
  
    For i = 1 To UBound(dataArr, 1)
        ct = 0
        outputArr(i) = ""
        For j = 1 To UBound(dataArr, 2)
            If Not IsEmpty(dataArr(i, j)) Then
                ct = ct + 1
                outputArr(i) = outputArr(i) & ct & ". " & dataArr(i, j) & Chr(10)
            End If
        Next j
        outputArr(i) = Left(outputArr(i), Len(outputArr(i)) - 1)
    Next i
  
    Range("D1").Resize(UBound(outputArr), 1).Value = Application.Transpose(outputArr)
  
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Sub PopulateColumn() Dim rng As Range Dim dataArr As Variant Dim outputArr() As String Dim i As Long, j As Long Dim ct As Long Application.ScreenUpdating = False Set rng = Range("F1:O3") dataArr = rng.Value ReDim outputArr(1 To UBound(dataArr, 1)) For i = 1 To UBound(dataArr, 1) ct = 0 outputArr(i) = "" For j = 1 To UBound(dataArr, 2) If Not IsEmpty(dataArr(i, j)) Then ct = ct + 1 outputArr(i) = outputArr(i) & ct & ". " & dataArr(i, j) & Chr(10) End If Next j outputArr(i) = Left(outputArr(i), Len(outputArr(i)) - 1) Next i Range("D1").Resize(UBound(outputArr), 1).Value = Application.Transpose(outputArr) Application.ScreenUpdating = True End Sub
Run-time error 5
Invalid Procedure call or argument

By the way the Range shall be F1:O503 and the date will flow in D1:D503
 
Upvote 0
I only had 3 rows of data to test. Forgot to change back. Which line gave you the error? Maybe try this. Change the sheet name to your sheet name

VBA Code:
Sub PopulateColumn()
    Dim ws As Worksheet
    Dim rng As Range
    Dim dataArr As Variant
    Dim outputArr() As String
    Dim i As Long, j As Long
    Dim ct As Long
 
    Application.ScreenUpdating = False
    Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change sheet name as needed.
    Set rng = ws.Range("F1:O503")
 
    dataArr = rng.Value
 
    ReDim outputArr(1 To UBound(dataArr, 1))
 
    For i = 1 To UBound(dataArr, 1)
        ct = 0
        outputArr(i) = ""
        For j = 1 To UBound(dataArr, 2)
            If Not IsEmpty(dataArr(i, j)) Then
                ct = ct + 1
                outputArr(i) = outputArr(i) & ct & ". " & dataArr(i, j) & Chr(10)
            End If
        Next j
        outputArr(i) = Left(outputArr(i), Len(outputArr(i)) - 1)
    Next i
 
    ws.Range("D1").Resize(UBound(outputArr), 1).Value = Application.Transpose(outputArr)
 
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,409
Messages
6,124,733
Members
449,185
Latest member
hopkinsr

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