VBA: Separate cell text to one word per line

DarkJester89

Board Regular
Joined
Nov 5, 2017
Messages
109
Office Version
  1. 2016
Platform
  1. Windows
Can anybody suggest a way, VBA to apply to a range of cells into one row (B2:CT2) so it's one word per line (in the same cell).

I have a code that extends the column out but it pushes all the text to one line, so it's too long.

VBA Code:
Sub AutoFitWrappedText()
    With Range("B2:CT2").EntireColumn
    .ColumnWidth = 255 ' maximum width
    .AutoFit
    End With
End Sub


Example Cell, what it says: Dog and Cat

Example Cell, what it looks like:

Code:
Do

g

an

cat

What I'm aiming for:

Code:
Dog

and

Cat

Any help is much appreciated and thank you in advance.
 
='User"&Char(10)&"Dashboard'!G2, so it's looking up a reference cell, not a direct hard-coded text in cell.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You could change the formula to
Excel Formula:
=SUBSTITUTE('User Dashboard'!G2," ",CHAR(10))
 
Upvote 0
Hi

VBA Code:
Sub AutoFitWrapped_Formlua_Text()
Dim Rng As Range
   With Range("B2:CT2")
   .WrapText = True
        For Each Rng In .Cells
         With Rng
              If .HasFormula And Not .Formula Like "*SUBSTITUTE*" Then
              .Replace .Formula, "=SUBSTITUTE(" & Replace(.Formula, "=", "") & ","" "",CHAR(10))", xlPart, , , , False, False
              End If
          End With
         Next
         .EntireColumn.AutoFit
         .EntireRow.AutoFit
   End With
End Sub
 
Upvote 0
Thank you, now of the cells will go beyond 8 width, is it the CHAR (10) that's restricting it because some of the larger words that would go past time are being wrapped. Thank you in advance!
 
Upvote 0
example.png
 
Upvote 0
Have you changed the formula, or are you using the code supplied by Dossfm0q in post#13?
 
Upvote 0
Check now please

Separate cell text to one word per line.gif

Before:


After:




VBA Code:
Sub AutoFitWrapped_Formlua_Text()
Application.ScreenUpdating = False
Dim Rng As Range
   With Range("B2:CT2")
  
        For Each Rng In .Cells
         With Rng
         .WrapText = False
              If .HasFormula And Not .Formula Like "*SUBSTITUTE*" Then
              .Replace .Formula, "=SUBSTITUTE(" & Replace(.Formula, "=", "") & ","" "",CHAR(10))", xlPart, , , , False, False
              End If
              .EntireColumn.AutoFit
              .WrapText = True
              .EntireColumn.AutoFit
              .HorizontalAlignment = xlCenter
              .VerticalAlignment = xlCenter

          End With
         Next
        
         .EntireRow.AutoFit
   End With
   Application.ScreenUpdating = True

End Sub
 
Upvote 0
Also you have more option to Wrap after slash "/"


WAS.gif

Code:
' Wrap after slah "/"'
Sub AutoFitWrapped_Formlua_Text()
Application.ScreenUpdating = False
Dim Rng As Range
   With Range("B2:CT2")
  
        For Each Rng In .Cells
         With Rng
         .WrapText = False
              If .HasFormula And Not .Formula Like "*SUBSTITUTE*" Then
             ' .Replace .Formula, "=SUBSTITUTE(" & Replace(.Formula, "=", "") & ","" "",CHAR(10))", xlPart, , , , False, False
              .Replace .Formula, "=SUBSTITUTE(SUBSTITUTE(" & Replace(.Formula, "=", "") & ","" "",CHAR(10)),""/"",""/""&CHAR(10))", xlPart, , , , False, False
              End If
              .EntireColumn.AutoFit
              .WrapText = True
              .EntireColumn.AutoFit
              .HorizontalAlignment = xlCenter
              .VerticalAlignment = xlCenter

          End With
         Next
        
         .EntireRow.AutoFit
   End With
   Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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