I found this great macro which adjusts the height of cells when a worksheet is activated (see macro below). I would like to activate the macro from a command button on a different worksheet within the same workbook, but I cannot get it to work. I tried adding the macro to a "Private Sub CommandButton1_Click()" function and included a line to activate the correct worksheet but it doesn't seem to work
THE ORIGINAL CODE THAT WORKS
Private Sub worksheet_activate()
Dim MergeWidth As Single
Dim cM As Range
Dim AutoFitRng As Range
Dim CWidth As Double
Dim NewRowHt As Double
Dim str01 As String
Dim k As Integer
'************************
'adjust row height for rigging according to wrapped text dimensions
For k = 13 To 27
str01 = "A" & k
If Rows(k).Hidden = False Then
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
Next k
End Sub
THE UPDATED CODE THAT DOESN"T WORK
Private Sub CommandButton1_Click()
Worksheets("CL-1 HEAD").Activate
Dim MergeWidth As Single
Dim cM As Range
Dim AutoFitRng As Range
Dim CWidth As Double
Dim NewRowHt As Double
Dim str01 As String
Dim k As Integer
'************************
'adjust row height for rigging according to wrapped text dimensions
For k = 13 To 27
str01 = "A" & k
If Rows(k).Hidden = False Then
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
Next k
End Sub
Bookmarks