Wrapping Text and Expanding Cell

EE Pros,

I hope this is a very simple question.   I need to activate Wrap Text in a particular Cell.  If I also want the cell to expand (change Row WIDTH), how do I get both of those things to happen?  I know how to activate Wrap Text, I just don't know how to set the default to also expand the row (Width) if the text exceeds the current Width.

That's it!

Thank you in advance.

B.
Bright01Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
you can write a macro like this:

Private Sub Workbook_Open()
    Call Macro1
End Sub

Sub Macro1()
    Range("D3").Select
    ActiveCell.FormulaR1C1 = "I need to activate Wrap Text in a particular Cell.  If I also want the cell to expand (change Row WIDTH), how do I get both of those things to happen?"
    Range("D3").Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("D:D").ColumnWidth = 22
End Sub
ProfessorJimJamMicrosoft Excel ExpertCommented:
you need a simple code in open workbook event

Private Sub Workbook_Open()

Dim CL As Range

For Each CL In ActiveSheet.UsedRange
CL.WrapText = True

        If CL.WrapText Then CL.Rows.AutoFit
    Next


End Sub

Open in new window



see it in the attached file.
EE.xlsm
Bright01Author Commented:
ProfessorJimJam,  This works well!!!  My question to you is, can I limit this capability to a single spreadsheet by putting the code in the Worksheet rather then This Workbook section?

Ryan, I have to fire the macro to get your example to work....... Also, the row width is static or preset... Is that right?

Thanks,

B.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

ProfessorJimJamMicrosoft Excel ExpertCommented:
Bright01

you can put this into the worksheet module.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c In ActiveSheet.UsedRange

c.WrapText = True

        If c.WrapText Then c.Rows.AutoFit
        
    Next
    
End Sub

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>I have to fire the macro to get your example to work....... Also, the row width is static or preset... Is that right?

yes, you need to run a macro in order to format your worksheet when the workbook is opened.

in this example, the row width is fixed. I just quickly give u an example, you can try customize accordingly.
Bright01Author Commented:
ProfessorJimJam,

I'm getting some sort of loop as I incorporate your changes into my production version.  Can you take a quick look?  Something is causing it to check the entire sheet or something.  You will see this as soon as you open it.
Thank you,

B.
Wrapping-Text-v2.xlsm
ProfessorJimJamMicrosoft Excel ExpertCommented:
Bright01

it is because you used the initial code in Workbook event.

i have now, used the code in my comment ID: 40836924 the worksheet change event.

plz see attached.
Wrapping-Text-v2.xlsm

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bright01Author Commented:
Great work!  Works like a charm.  I will be authoring another question using this worksheet.  Hope you will join in.

Thanks again,


B.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.