I have a little problem in excel. I not experienced with excel macros and would be grateful for some help. I am trying to find a macro which ajustes the height of a merged c
What about this:
'rRang is range of cells which are merged together
Sub AutoFitRowMergedCells(rRang As Range)
Dim iColW As Integer, iColWold As Integer, I As Integer
iColW = 0
For I = 1 To rRang.Columns.Count
iColW = iColW + rRang.Range("A" & I).ColumnWidth
Next I
rRang.UnMerge
iColWold = rRang.Range("A1").ColumnWidth
rRang.Range("A1").ColumnWidth = iColW
rRang.Range("A1").EntireRow.AutoFit
rRang.Range("A1").ColumnWidth = iColWold
rRang.Merge
End Sub
Something like:
Dim h, rng As Range
Set rng = Selection
With rng
.UnMerge
.Cells(1).EntireRow.AutoFit
h = .Cells(1).RowHeight
.Merge
.EntireRow.AutoFit
With .Cells(1).MergeArea
.Cells(.Cells.Count).RowHeight = _
.Cells(.Cells.Count).RowHeight + (h - .Height)
End With
End With
There is a much easier way of doing this if you allow the Excel sheet to do some of the heavy lifting for you.
The following example works in the common scenario that you have some cells that comprise several columns but only a single row (i.e. some columns are merged together on a single row). The usual problem is that the row height for wrapped text in the merged cell does not accomodate the height of the wrapped text in some circumstances (e.g. the result of a formula or database lookup gives a large and varying amounts of text)
To solve this, simulate single celled versions of the merged cells by doing the following in some columns that are not visible to the user:
Write a function that loops through all of these named single cell ranges and calls the following function for each:
Private Sub AutosizeLongFormInput(rng As Range)
If Not rng.EntireRow.Hidden = True Then
rng.EntireRow.AutoFit
End If
End Sub