|
-
Nov 15th, 2009, 12:00 AM
#1
Thread Starter
Addicted Member
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...
-
Nov 15th, 2009, 01:38 AM
#2
Re: Wrap text
i played with this, but it is not correct
vb Code:
Dim r As Range, myarr() As String Set r = Range("A3") linecnt = Len(r) \ r.ColumnWidth ReDim myarr(linecnt) awords = Split(r) tmp = awords(0) & " " j = 0 For i = 1 To UBound(awords) If Len(tmp) + Len(awords(i)) > r.ColumnWidth + 1 Then myarr(j) = tmp j = j + 1 tmp = awords(i) & " " Else tmp = tmp & awords(i) & " " End If Next 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
-
Nov 16th, 2009, 06:04 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|