Hello, Im relatively new to VBA codes. I usually manage to work these things out myself, but cant seem to crack it this time...!
Im using a VBA code to autosize row height for merged cells. The code (below) works when I run it, but otherwise doesnt seem to update.
Are there any hints or tips out there to get it running?
Thanks!!
Im using a VBA code to autosize row height for merged cells. The code (below) works when I run it, but otherwise doesnt seem to update.
Code:
Private Sub Worksheet_Activate()
Public Sub AutoFitAll()
Call AutoFitMergedCells(Range("a20:d20"))
Call AutoFitMergedCells(Range("a22:d22"))
Call AutoFitMergedCells(Range("a24:d24"))
End Sub
Public Sub AutoFitMergedCells(oRange As Range)
Dim tHeight As Integer
Dim iPtr As Integer
Dim oldWidth As Single
Dim oldZZWidth As Single
Dim newWidth As Single
Dim newHeight As Single
With Sheets("Objective Setting (Dec)")
oldWidth = 0
For iPtr = 1 To oRange.Columns.Count
oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth
Next iPtr
oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth
oRange.MergeCells = False
newWidth = Len(.Cells(oRange.Row, oRange.Column).Value)
oldZZWidth = .Range("ZZ1").ColumnWidth
.Range("ZZ1") = Left(.Cells(oRange.Row, oRange.Column).Value, newWidth)
.Range("ZZ1").WrapText = True
.Columns("ZZ").ColumnWidth = oldWidth
.Rows("1").EntireRow.Autofit
newHeight = .Rows("1").RowHeight / oRange.Rows.Count
.Rows(CStr(oRange.Row) & ":" & CStr(oRange.Row + oRange.Rows.Count - 1)).RowHeight = newHeight
oRange.MergeCells = True
oRange.WrapText = True
.Range("ZZ1").ClearContents
.Range("ZZ1").ColumnWidth = oldZZWidth
End With
End Sub
Are there any hints or tips out there to get it running?
Thanks!!
Last edited by a moderator: