Convert multiple text lines in 1 cell to an unordered list

rajsidhu

New Member
Joined
Mar 8, 2022
Messages
7
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I read a post in this forum that shows how to make multiple text lines, in 1 cell, into bullet points which works great. Here is the code:-

VBA Code:
Sub aTest()
    Dim rCell As Range, spl As Variant, i As Long
    
    For Each rCell In Selection
        spl = Split(rCell, Chr(10))
        For i = LBound(spl) To UBound(spl)
            spl(i) = Chr(60) & "li" & Chr(62) & spl(i) & Chr(60) & "/li" & Chr(62)
        Next i
        rCell = Join(spl, Chr(10))
    Next rCell
    Selection.ColumnWidth = 200
    Selection.EntireRow.AutoFit
    Selection.EntireColumn.AutoFit
End Sub

What i need to do is create an unordered list out of these bullet points.

Is there any one who can help me modify the above code so that it spits out an unordered list of bullet points please? (y)
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I assume by "an unordered list" you mean the code should return the bullets in a random order, not necessarily in the exact order in which they appear prior to code execution.
Lightly tested, but I think this will do that:
VBA Code:
Sub aTest()
    Dim rCell As Range, splIN As Variant, splOUT As Variant, i As Long, j As Long, d As Object
    Set d = CreateObject("Scripting.dictionary")
    For Each rCell In Selection
        d.RemoveAll
        splIN = Split(rCell, Chr(10))
        ReDim splOUT(LBound(splIN) To UBound(splIN))
        For i = LBound(splIN) To UBound(splIN)
Again:      j = WorksheetFunction.RandBetween(LBound(splIN), UBound(splIN))
            If Not d.exists(j) Then
                d.Add j, d.Count
                splOUT(j) = Chr(60) & "li" & Chr(62) & splIN(i) & Chr(60) & "/li" & Chr(62)
            ElseIf d.Count < UBound(splIN) + 1 Then
                GoTo Again
            End If
        Next i
        rCell = Join(splOUT, Chr(10))
    Next rCell
Selection.ColumnWidth = 200
Selection.EntireRow.AutoFit
Selection.EntireColumn.AutoFit
End Sub
 
Upvote 0
I assume by "an unordered list" you mean the code should return the bullets in a random order, not necessarily in the exact order in which they appear prior to code execution.
Lightly tested, but I think this will do that:
VBA Code:
Sub aTest()
    Dim rCell As Range, splIN As Variant, splOUT As Variant, i As Long, j As Long, d As Object
    Set d = CreateObject("Scripting.dictionary")
    For Each rCell In Selection
        d.RemoveAll
        splIN = Split(rCell, Chr(10))
        ReDim splOUT(LBound(splIN) To UBound(splIN))
        For i = LBound(splIN) To UBound(splIN)
Again:      j = WorksheetFunction.RandBetween(LBound(splIN), UBound(splIN))
            If Not d.exists(j) Then
                d.Add j, d.Count
                splOUT(j) = Chr(60) & "li" & Chr(62) & splIN(i) & Chr(60) & "/li" & Chr(62)
            ElseIf d.Count < UBound(splIN) + 1 Then
                GoTo Again
            End If
        Next i
        rCell = Join(splOUT, Chr(10))
    Next rCell
Selection.ColumnWidth = 200
Selection.EntireRow.AutoFit
Selection.EntireColumn.AutoFit
End Sub
Thanks JoMoe for replying.

What i need is my bullet points wrapped in <ul>.....</ul>

So that this (which is all in one cell)

Min./Max. Saddle Height: 170/576mm.
Chassis Length: 1605mm.
European style trolley jack manufactured to exacting standards.
Lifting arm has 2-speed lifting and lowering; lowers rapidly without load and provides smooth, slow, controlled lowering with load.
Lift-and-twist 'Dead Man' mechanism operated via 'D' shaped handle prevents accidental lowering.
Nylon wheels fitted which are quiet in operation and help prevent damage to garage floors.
Foot pedal for quick approach of the lifting saddle to the vehicle.
Includes removable rubber saddle pad and built-in tool tray for fasteners and tools.
This item is heavy. Extra assistance must be provided at the delivery point to help its safe delivery.
Model No. 10000ES

Becomes this (all in one cell)

<ul>
<li> Min./Max. Saddle Height: 170/576mm.</li>
<li> Chassis Length: 1605mm.</li>
<li> European style trolley jack manufactured to exacting standards.</li>
<li> Lifting arm has 2-speed lifting and lowering; lowers rapidly without load and provides smooth, slow, controlled lowering with load.</li>
<li> Lift-and-twist 'Dead Man' mechanism operated via 'D' shaped handle prevents accidental lowering.</li>
<li> Nylon wheels fitted which are quiet in operation and help prevent damage to garage floors.</li>
<li> Foot pedal for quick approach of the lifting saddle to the vehicle.</li>
<li> Includes removable rubber saddle pad and built-in tool tray for fasteners and tools.</li>
<li> This item is heavy. Extra assistance must be provided at the delivery point to help its safe delivery.</li>
<li> Model No. 10000ES</li>
</ul>

I tried your code but it is only wrapping each line with <li>....</li>

I'm hoping that it would only take a small modification to your code to add <ul> to the start of the cell contents and </ul> to the end of the cell contents ?
 
Upvote 0
If all you want to do is add the outer tags, how about this mod to the code you posted
VBA Code:
rCell = "<ul>" & Chr(10) & Join(spl, Chr(10)) & Chr(10) & "</ul>"
But I still have no idea what you mean by
an unordered list of bullet points
 
Upvote 0
If all you want to do is add the outer tags, how about this mod to the code you posted
VBA Code:
rCell = "<ul>" & Chr(10) & Join(spl, Chr(10)) & Chr(10) & "</ul>"
But I still have no idea what you mean by
Yes i need to add <ul> and </ul> as the outer tags as you said.

Where in the code should i add the line
VBA Code:
rCell = "<ul>" & Chr(10) & Join(spl, Chr(10)) & Chr(10) & "</ul>"
 
Upvote 0
Replace this line with the code I posted
VBA Code:
rCell = Join(spl, Chr(10))
 
Upvote 0
Replace this line with the code I posted
VBA Code:
rCell = Join(spl, Chr(10))
I don't see that line.

I do see this line
VBA Code:
rCell = Join(splOUT, Chr(10))

However when i replaced this line with your code, there was an error.
 
Upvote 0
What exactly is "unordered" about the desired result you show in post #4?
What is the error message and what line is highlighted when you get the error you refer to in post #8?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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