|
从外国人那里抄回来的,你看一下。
- Sub 调整合并单元格高度()
-
-
- Dim j As Long
- Dim n As Long
- Dim i As Long
- Dim MW As Double 'merge width
- Dim RH As Double 'row height
- Dim MaxRH As Double
- Dim rngMArea As Range
- Dim rng As Range
-
- Const SpareCol As Long = 26
- Set rng = Range("a1:m68" & _
- Range("A" & Rows.Count).End(xlUp).row)
-
- With rng
- For j = 1 To .Rows.Count
- 'if the row is not hidden
- If Not .Parent.Rows(.Cells(j, 1).row) _
- .Hidden Then
- 'if the cells have data
- If Application.WorksheetFunction _
- .CountA(.Rows(j)) Then
- MaxRH = 0
- For n = .Columns.Count To 1 Step -1
- If Len(.Cells(j, n).value) Then
- 'mergecells
- If .Cells(j, n).MergeCells Then
- Set rngMArea = _
- .Cells(j, n).MergeArea
- With rngMArea
- MW = 0
- If .WrapText Then
- 'get the total width
- For i = 1 To .Cells.Count
- MW = MW + _
- .Columns(i).ColumnWidth
- Next
- MW = MW + .Cells.Count * 0.66
- 'use the spare column
- 'and put the value,
- 'make autofit,
- 'get the row height
- With .Parent.Cells(.row, SpareCol)
- .value = rngMArea.value
- .ColumnWidth = MW
- .WrapText = True
- .EntireRow.AutoFit
- RH = .RowHeight
- MaxRH = Application.Max(RH, MaxRH)
- .value = vbNullString
- .WrapText = False
- .ColumnWidth = 8.43
- End With
- .RowHeight = MaxRH
- End If
- End With
- ElseIf .Cells(j, n).WrapText Then
- RH = .Cells(j, n).RowHeight
- .Cells(j, n).EntireRow.AutoFit
- If .Cells(j, n).RowHeight < RH Then _
- .Cells(j, n).RowHeight = RH
- End If
- End If
- Next
- End If
- End If
- Next
- .Parent.Parent.Worksheets(.Parent.Name).UsedRange
- End With
- Set rng = Nothing
- End Sub
复制代码 |
|