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.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
How about
VBA Code:
Sub AutoFitWrappedText()
   With Range("B2:CT2")
      .Replace " ", vbLf, xlPart, , , , False, False
      .EntireColumn.AutoFit
   End With
End Sub
 
Upvote 0
I'm not sure what happened with that, but the formulas in that range did NOT like what happened, it ref # out and corrupted the formulas
 
Upvote 0
You never said anything about formulae. ;)
What is the formula?
 
Upvote 0
Oh, I was trying to keep it as a VBA code because the cells already have formula, it was much appreciated though :)
 
Upvote 0
It may be possible, by changing the formula if that is ok?
 
Upvote 0
Example
Code:
=IF(1=1,"cat and dog","")
It work with Fluff Code below
VBA Code:
Sub AutoFitWrappedText()
   With Range("B2:CT2")
      .Replace " ", """&Char(10)&""", xlPart, , , , False, False
      .WrapText = True
      .EntireColumn.AutoFit
      .EntireRow.AutoFit
      
   End With
End Sub
 
Upvote 0
@Dossfm0q The VBA is replaced my formulas with the code you made, in essence, this closed vba process works, but can I get it to work with a code that's already in place?

VBA Code:
='User Dashboard'!G2

An example, and if the reference cell, G2 is empty, it would be auto-hid, which I'm not sure if it has to be converted to search for visible columns.
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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