Results 1 to 5 of 5

Thread: Solved: Wrapping text question

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    VBAX Expert
    Joined
    Feb 2005
    Posts
    924
    Location
    Quote Originally Posted by malik641
    I have a drop down list in cell A2. Cell B2 has a formula that will choose another cell based on A2. The formula is:

    PHP Code:
    =IF(A2=$K$2,$L$2,IF(A2=$K$3,$L$5,IF(A2=$K$4,$L$8,IF(A2=$K$5,$L$11,"")))) 
    The problem is when I choose a value for A2, the formula works fine, but there is too much text for the cell. I formatted the cells for text wrapping, but it doesn't work with the formula (unless A2 has a value and I click on the formula in B2 and press F9 then Enter...but that's inefficient). Can I make a macro to adjust the row for the correct size to fit all the text, based on this information??

    Thanks in advance!
    I think you are asking two questions:
    1. can a macro set "word wrap"?
    2. can a macro adjust the width of the column to accomodate the text?

    The answer to both is yes. For the first, you might try something like:
    Sub WordWrap()
    Range("A2").Select
        With Selection
            .VerticalAlignment = xlCenter
            .WrapText = True
        End With
    End Sub
    I use a centered vertical alignment whenever I wrap text (looks better to me), but you may not wish to do that.

    For the second, the width of the column will depend on the amount of text and the particular font & font size being used. If there is more than a dozen or so characters, you should use a combination of col width and word wrap. An example of setting col width based on text found in cell A2 might look like:
    Sub ColWidth()
        Columns("A:A").ColumnWidth = Len(Range("A2").Text) / 5
    End Sub
    For the font and font size I was using (Arial 10), setting the width to # chars / 5 seemed like a good compromise.

    I assume you know how to convert your cascaded IF statement to VBA and how to combine the ideas above. Also, remember that setting the col width impacts every cell in that col.
    Last edited by Aussiebear; 04-27-2023 at 07:46 PM. Reason: Adjusted the code tags
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •