« セルの内容を90度回転、-90度回転、180度回転 | トップページ | For文の使い方とテンプレート »

2018年2月25日 (日)

Rotate cell contents 90 degrees, -90 degrees and 180 degrees

There is an opportunity to use Excel to make a seat chart, an arrangement map of the office and so on.

"I made up my mind what I should make it upwards, but I made it for the time"
"The opposite direction was better after all"
Or
"It is difficult to see the seat table as seen from the seat where I sit.
"So, I want to change the direction of it.
Have you ever thought about the above?

In such a case,
You should want to rotate the contents of the cell by 90 degrees or 180 degrees (clockwise as positive direction).

I would like to make a macro that can be used in such cases.

To rotate cell contents by 90 degrees
What kind of operation is required?

Please look at the figure below.
What is the operation necessary to rotate the arrangement on the left by 90 degrees and place it like the right side?

Vba20180212a

First, the rows on the left (A, B, C, D) are in the direction of the column.
The first row (A, B, C, D) before change is the second row.
The second row is in the direction of the column, as same as  the former first row.
the second row before the change is The first column.

In other words,
you can swap rows and columns and reverse the columns themselves It is to rotate cell contents by 90 degrees.

Rotation by -90 degrees is as shown below
Swap rows and columns
Make the contents of the column reverse.

Vba20180212b

The operation of rotating by 90 degrees is
the same as swapping rows and columns or putting them in reverse order.
It is a strange feeling that rotation can be done by swapping.

Next I will think about the operation to rotate by 180 degrees.
Please see the figure below.

Vba20180212c

The first row (A, B, C, D) is in reverse order
Furthermore, the columns themselves are also in reverse order.

To do this with VBA

Make the contents of the columns reverse
Reverse the order of each column.

In the case of 180 degree rotation,
Do not swap rows and columns
but make it in reverse order.

To do these operations with VBA,
After storing the selected range in the array
We will exchange with For statements and UBound.
For the method of storing the contents of the cell in an array
Please also refer to this article.
Store cell values in arrays

 

Code here

Code that rotates cell contents by 90 degrees:

Sub macro180211a()
'Rotate cell contents by 90 degrees
'Run macro after choosing the range you want to rotate
'Output based on cell A1

    Dim i As Integer, j As Integer
    Dim vData As Variant
   
    'Store selection range value in Variant type variable
    vData = Selection
   
    For i = 1 To UBound(vData, 1)
        For j = 1 To UBound(vData, 2)
            'Reverse cell contents of rows and swap rows and columns
            Cells(j, UBound(vData, 1) - (i - 1)) = vData(i, j)
        Next j
    Next i
       
End Sub


Code that rotates cell contents by -90 degrees:

Sub macro180211b()
'Rotate cell contents by -90 degrees
'Run macro after choosing the range you want to rotate
'Output based on cell A1

    Dim i As Integer, j As Integer
    Dim vData As Variant
   
    'Store selection range value in Variant type variable
    vData = Selection
   
    For i = 1 To UBound(vData, 1)
        For j = 1 To UBound(vData, 2)
            'Swap rows and columns and reverse cell contents of columns
            Cells(UBound(vData, 2) - (j - 1), i) = vData(i, j)
        Next j
    Next i
   
End Sub


Code that rotates cell contents by 180 degrees:

Sub macro180211c()
'Rotate cell contents 180 degrees
'Select the range you want to rotate and execute macro
'Output based on cell A1

    Dim i As Integer, j As Integer
    Dim vData As Variant
   
    'Store selection range value in Variant type variable
    vData = Selection
   
    For i = 1 To UBound(vData, 1)
        For j = 1 To UBound(vData, 2)
            'Reverse the cell contents of the column and reverse the order of each column
            Cells(UBound(vData, 1) - (i - 1), _
                UBound(vData, 2) - (j - 1)) = vData(i, j)
        Next j
    Next i
   
End Sub

By the way
to make it a mirror image,
To make a mirror image in the row direction, reverse the cell contents of the row.
In the column direction, reverse the cell contents of the column.
If you are interested, please modify the above code and make it.

|

« セルの内容を90度回転、-90度回転、180度回転 | トップページ | For文の使い方とテンプレート »

コメント

この記事へのコメントは終了しました。

トラックバック


この記事へのトラックバック一覧です: Rotate cell contents 90 degrees, -90 degrees and 180 degrees:

« セルの内容を90度回転、-90度回転、180度回転 | トップページ | For文の使い方とテンプレート »