Results 1 to 3 of 3

Thread: Wrap text

  1. #1

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Post Wrap text

    Suppose you have a cell with the following value: "Line one and line 2". You set the format of the cell to wrap text and you change its size so that it looks like:

    Line one and
    line 2

    Is it possible in vba to extract tomething like "Line one and" & vbLine & "line 2"?
    I think you can somehow deduce how many rows the cell has if you use my_rows=round(cell(1,1).height / cell(1,1).font.size, 0) so you can actually know how many vbLine to use. But still it's incomplete...

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Wrap text

    i played with this, but it is not correct
    vb Code:
    1. Dim r As Range, myarr() As String
    2. Set r = Range("A3")
    3. linecnt = Len(r) \ r.ColumnWidth
    4. ReDim myarr(linecnt)
    5. awords = Split(r)
    6. tmp = awords(0) & " "
    7. j = 0
    8. For i = 1 To UBound(awords)
    9.     If Len(tmp) + Len(awords(i)) > r.ColumnWidth + 1 Then
    10.         myarr(j) = tmp
    11.         j = j + 1
    12.         tmp = awords(i) & " "
    13.     Else
    14.         tmp = tmp & awords(i) & " "
    15.     End If
    16. Next
    17. myarr(j) = tmp
    unfortunately vba has no objects with textwidth property to work with, so i do not know anyway to get the accurate width of a string
    also excel will wrap words at point where there is no space if the word is too long and i did not test for that, though it could be done
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Wrap text

    i have been able to make this work, but it will return the lines as displayed in cells edit mode may change slightly after leaving the cell

    requires use of activex dll
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width