Overview
Rotates a 2D range 90 degrees clockwise, with an optional count parameter for 180 or 270 degree rotations. Unlike TRANSPOSE, this performs a true geometric rotation of the data matrix.
How It Works
- Arr: The 2D range to rotate.
- times (optional): Number of 90-degree clockwise rotations (default 1). Uses
Mod 4so values > 4 wrap around. - Remaps
Rtt(Row, Col)→Tmp(Col, Row_Cnt - Row + 1)for each rotation.
Usage
=fn_ROTATE(A1:D3) ' Rotate 90° clockwise =fn_ROTATE(A1:D3, 2) ' Rotate 180° =fn_ROTATE(A1:D3, 3) ' Rotate 270° (90° counter-clockwise)
The VBA Code
Function fn_ROTATE(Arr As Range, Optional times As Integer = 1) As Variant
' Rotates a 2D range 90 degrees clockwise, repeated 'times' times
Dim Rtt As Variant, Tmp As Variant
Dim Row_Cnt As Long, Col_Cnt As Long, Row As Long, Col As Long, Idx As Long
Rtt = Arr.Value
times = times Mod 4
For Idx = 1 To times
Row_Cnt = UBound(Rtt, 1): Col_Cnt = UBound(Rtt, 2)
ReDim Tmp(1 To Col_Cnt, 1 To Row_Cnt)
For Row = 1 To Row_Cnt
For Col = 1 To Col_Cnt
Tmp(Col, Row_Cnt - Row + 1) = Rtt(Row, Col)
Next Col
Next Row
Rtt = Tmp
Next Idx
fn_ROTATE = Rtt
End Function