'// No built-in "copy height" function in PasteSpecial like
'// there is for copying widths.
'// This code allows for the source range to have multiple cells, like
'// the built-in copy width function. Cycles through source, so if
'// source has three rows and target nine, the heights follow
'// the pattern of a,b,c,a,b,c,a,b,c.
'// Greg Truby, November 2006
Option Explicit
'// these variable names are long; but the suffix should avoid conflicts
'// with other variable names.
Public g_rngSource_copyrows As Range
Public g_rngTarget_copyrows As Range
Sub SetSourceToSelection()
'// for interactive use
If TypeName(Selection) = "Range" Then Set g_rngSource_copyrows = Selection
If Not g_rngTarget_copyrows Is Nothing Then CopyRowHeights
End Sub
Sub SetTargetToSelection()
'// for interactive use
If TypeName(Selection) = "Range" Then Set g_rngTarget_copyrows = Selection
If Not g_rngSource_copyrows Is Nothing Then CopyRowHeights
End Sub
Sub CallFromCode()
'// how to use in VBA
Set g_rngSource_copyrows = Sheets(2).Range("A1:A2")
Set g_rngTarget_copyrows = Sheets(1).Range("A1:A12")
CopyRowHeights
End Sub
Private Sub CopyRowHeights()
Dim lngRow As Long
If g_rngSource_copyrows Is Nothing _
And g_rngTarget_copyrows Is Nothing Then GoTo ErrorMsg
If g_rngSource_copyrows Is Nothing Then
If vbNo = MsgBox("No source range set, use current selection?", _
vbQuestion + vbYesNo, "Source=Current") Then
GoTo ErrorMsg ' |--¿goto?-->
End If
If TypeName(Selection) <> "Range" Then GoTo ErrorMsg ' |--¿goto?-->
Set g_rngSource_copyrows = Selection
ElseIf g_rngTarget_copyrows Is Nothing Then
If vbNo = MsgBox("No target range set, use current selection?", _
vbQuestion + vbYesNo, "Target=Current") Then
GoTo ErrorMsg ' |--¿goto?-->
End If
If TypeName(Selection) <> "Range" Then GoTo ErrorMsg ' |--¿goto?-->
Set g_rngTarget_copyrows = Selection
End If
For lngRow = 1 To g_rngTarget_copyrows.Rows.Count
g_rngTarget_copyrows.Cells(lngRow, 1).RowHeight = _
g_rngSource_copyrows.Cells((lngRow - 1) Mod g_rngSource_copyrows.Rows.Count + 1, 1).RowHeight
Next lngRow
Set g_rngSource_copyrows = Nothing
Set g_rngTarget_copyrows = Nothing
Exit Sub
ErrorMsg:
'"""""""
MsgBox "Cannot copy row heights.", vbCritical, "Operation Canceled"
End Sub