VBA: Separate cell text to one word per line

DarkJester89

Board Regular
Joined
Nov 5, 2017
Messages
94
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.
 

Some videos you may like

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,838
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub AutoFitWrappedText()
   With Range("B2:CT2")
      .Replace " ", vbLf, xlPart, , , , False, False
      .EntireColumn.AutoFit
   End With
End Sub
 

DarkJester89

Board Regular
Joined
Nov 5, 2017
Messages
94
Office Version
  1. 2016
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,838
Office Version
  1. 365
Platform
  1. Windows
You never said anything about formulae. ;)
What is the formula?
 

DarkJester89

Board Regular
Joined
Nov 5, 2017
Messages
94
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

Oh, I was trying to keep it as a VBA code because the cells already have formula, it was much appreciated though :)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,838
Office Version
  1. 365
Platform
  1. Windows
It may be possible, by changing the formula if that is ok?
 

DarkJester89

Board Regular
Joined
Nov 5, 2017
Messages
94
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

No, it's pulling data, I can't alter the formula :(
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,838
Office Version
  1. 365
Platform
  1. Windows

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
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
 

DarkJester89

Board Regular
Joined
Nov 5, 2017
Messages
94
Office Version
  1. 2016
Platform
  1. Windows
@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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,380
Messages
5,595,832
Members
414,024
Latest member
Woodsa

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
Top