LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 905
Default How to autofit text containing CHR(10)?

"joel" wrote:
Multirow only applies the controls and not the worksheet.
Use wrap text to the worksheet.
Range("A1").WrapText = True


I had high hopes for this suggestion because it rang true.

But setting WrapText did nothing.

On the off-chance I did something wrong, see the complete modified macro
below. If you have a correction, please snip and correct the lines from the
macro to be specific. And please test your suggestion. I couldn't have
made it any easier to do.

I tried setting WrapText a few different ways separately. Again, none
worked for me.

First, after the loop (my preference):

Range(cellName).Resize(1, nCol).Columns.WrapText = True
Range(cellName).Resize(1, nCol).Columns.AutoFit

Note: I also altered the AutoFit line, changing Resize(nRow,nCol) to
Resize(1,nCol). I think the latter is sufficient. But I tried it both
ways, to no avail.

Alternatively, in the loop:

Range(cellName).Cells(r, c).WrapText = True
Range(cellName).Cells(r, c) = s

Finally, alternatively, in the loop:

Range(cellName).Cells(r, c).NumberFormat = "@"
Range(cellName).Cells(r, c).WrapText = True
Range(cellName).Cells(r, c) = s


The modified macro....


Option Explicit
#Const multirow = True
#Const kludge1fit = False
#Const kludge2fit = False

Private Sub doit()
Const cellName As String = "B2"
Const nRow As Long = 3
Const nCol As Long = 3
Dim r As Long, c As Long, i As Long, t As String, s As String
Dim maxLen As Integer, x As Long

Range("a:z").Delete
Range("a1").Select

#If kludge1fit And Not kludge2fit Then
'set ColumnWidth before storing text
Range(cellName).Resize(1, nCol).ColumnWidth = 100
#End If

For r = 1 To nRow: For c = 1 To nCol
t = String(r * c, "x")
s = "morelongtext1" & t
#If multirow Then
For i = 2 To r * c
s = s & Chr(10) & "morelongtext" & String(i, 48 + i) & t
Next i
#End If
Range(cellName).Cells(r, c).NumberFormat = "@" 'workaround
Range(cellName).Cells(r, c).WrapText = True 'workaround
Range(cellName).Cells(r, c) = s
Next c: Next r

#If kludge2fit Then
'set ColumnWidth after storing text
x = nRow * nCol
maxLen = Len("morelongtext" & String(x, 48 + x) & t)
Range(cellName).Resize(1, nCol).ColumnWidth = maxLen
#End If

Range(cellName).Resize(1, nCol).Columns.AutoFit
End Sub

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rows().AutoFit starts a calculation, but Columns().AutoFit doesn't Stefano[_2_] Excel Programming 1 November 30th 09 05:01 PM
Rows().AutoFit starts a calculation, but Columns().AutoFit doesn't Mike H Excel Programming 0 November 25th 09 04:31 PM
autofit text when resizing text box in Excel 2007 tinks Excel Discussion (Misc queries) 0 May 4th 09 04:20 PM
Autofit text into a cell rp2chil Excel Discussion (Misc queries) 1 May 11th 07 09:07 AM
Autofit, Centre and Text Paul Black[_2_] Excel Programming 5 February 1st 05 05:28 PM


All times are GMT +1. The time now is 01:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"