Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Excel - Word Wrap

I'm creating a small piece of VBA code with a specific formula, however it has a couple of if statements, one of which originates a double-line string (with vbNewLine) The issue is that I can't see the text.

So I wanted to word wrap it, but each time I set the ActiveCell.WrapText = True, nothing happens.

I checked with a message box. I set the WrapText to True, I return the property value with the MessageBox to confirm, and it's still False.

I've been told to use ActiveCell.Rows.AutoFit as well, but AutoFit does nothing if the text isn't wrapped.

Any idea what I might be doing wrong here?

like image 512
Kuroh Avatar asked May 03 '11 10:05

Kuroh


People also ask

Can you set Excel to automatically wrap text?

Wrap text automatically In a worksheet, select the cells that you want to format. On the Home tab, in the Alignment group, click Wrap Text. (On Excel for desktop, you can also select the cell, and then press Alt + H + W.)


2 Answers

For me, the code below worked. (only set to change header row, (change range))

ActiveSheet.Range("A1:R1").Select
With Selection
    .WrapText = True
End With
like image 54
user7775323 Avatar answered Oct 13 '22 13:10

user7775323


try:

Sub WrapandFit()

    ActiveCell.WrapText = True
    ActiveCell.EntireRow.AutoFit

End Sub

It worked for me. Make sure that your screenupdating is also set to true.

like image 44
GrimR7529 Avatar answered Oct 13 '22 14:10

GrimR7529