views:

225

answers:

3

The problem: I use VBA to populate MS Excel 2000 cells with text. The column has a fixed width (should not be changed due to layout) and the wordwrap property is set to true so the text wraps over multiple lines if wider than the column. Unfortunately, the row-height do not always get updated accordingly. I need a way to predict if the text wraps over multiple lines so I can "manually" adjust the height.

I want to do something like this:

range("A1").value = longText  
range("A1").EntireRow.RowHeight = 14 * GetNrOfLines(range("A1"))

How do I write the function GetNrOfLines?

I can't rely on number of characters since the font is not mono-space. Sometimes the cells I'm writing to are merged with other cells so Autofit doesn't work. Please remember that I'm working in Excel 2000. Suggestions?

A: 

What about using the Range.Rows.AutoFit method?

Sam Russo
A: 

You say that AutoFit won't work because the cell is sometimes merged (with the cell above or below, I presume).

However, you could create a temporary worksheet, copy the content and formatting (column width, font, size, etc.) of the cell over there, then use AutoFit to get the ideal row height? Then delete the temporary worksheet again. (If you're doing lots of cells at once, then obviously you can use the temporary worksheet for them all without re-creating it each time).

Gary McGill
Thanks for the reply. The merge is actually across columns, but still autofit fails most of the time. Regarding your suggestion. I've thought along the same lines -with the extra spread sheet and all- but it isn't really a viable solution. I've done some tests and it seems that excel some times comes into an error state where autofit doesn't work (it applies it self on the first row). The strange state seems to be more likely to appear if the workbook is an add-in and application.screenupdating is turned off
Zen
A: 

Unfortunately, I haven't found a good solution. The problem originates in a bug in Excel 2000. I do not know if it also applies to later versions.

The problem manifest it self when merging cells horizontally. Row height fails to auto adjust when you have merged cells.

The following example code shows the problem

Dim r As Range
Set r = Sheet1.Range("B2")
Range(r, r(1, 2)).Merge
r.Value = ""
r.Value = "asdg lakj dsgl dfgjdfgj dgj dfgj dfgjdgjdfgjdfgjd"
r.WrapText = True
r.EntireRow.AutoFit

In this case r.EntireRow.AutoFit will not take into account that the text spans over several rows, and adjust the height as if it was single line of text.
You'll have the same problem when doing manual autofit (double clicking on the row-height-adjuster in the sheet) to a row that has merged cells and word wrap.

A solution (as suggested by Gary McGill ) is to use an unrelated sheet. Set the column width to match the full with of the merged cells. Copy the text, with the same formating. Let the cell auto-adjust and use that cells values.

Here follows a simplified example:

Public Sub test()

    Dim widthInPoints As Double
    Dim mergedCells As Range
    Set mergedCells = Sheet1.Range("B2:C2")
    widthInPoints = mergedCells.width

    Dim testCell As Range
    Set testCell = Sheet2.Range("A1")
    testCell.EntireColumn.columnWidth = ConvertPointsToColumnWidth(widthInPoints, Sheet2.Range("A1"))
    testCell.WrapText = True
    testCell.Value = mergedCells.Value
    'Text formating should be applied as well, if any'

    testCell.EntireRow.AutoFit

    mergedCells.EntireRow.rowHeight = testCell.rowHeight
End Sub

Private Function ConvertPointsToColumnWidth(widthInPoints As Double, standardCell As Range) As Variant

    ConvertPointsToColumnWidth = (widthInPoints / standardCell.width) * standardCell.columnWidth

End Function
Zen