« 2018年1月 | トップページ | 2018年3月 »

2018年2月

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.

| | コメント (0) | トラックバック (0)

2018年2月12日 (月)

セルの内容を90度回転、-90度回転、180度回転

Excelで座席表やオフィスの配置図などを作ることありますよね。

どちらを上にして作ればいいのか迷ってとりあえず作ったけど
やっぱりこっちの向きが良かった
とか
自分の座っている席から見てこの向きの座席表だと見難い
など

セルの内容を
90度回転(時計回りをプラス方向)させたり、
180度回転させたりしたい
と思ったことありませんか?

こんな場合に使えるマクロを作りたいと思います。

セルの内容を90度回転させるとは
どういう操作なのでしょうか?

下図の左側の配置を右側のように
90度回転させるために必要な操作は何でしょうか?

Vba20180212a

まず左側の行(A,B,C,D)が列の向きになって
(A,B,C,D)が1行目だったのが2列目になります。
2番目の行は1番目の行と同様に列の向きになって
2行目だったのが1列目になります。

つまり行と列を入れ替えて、列自体を逆順にすることが
セルの内容を90度回転させることです。

-90度回転は下図のとおり
行と列を入れ替えて
列の内容を逆順にする。

Vba20180212b

90度回転するという操作が
実は、
行と列の入れ替えや逆順にするとできる
というのは不思議な感じです。

では180度回転させるとはどういう操作でしょうか?
下図を見てください。

Vba20180212c

1行目(A,B,C,D)が逆順になって
さらに列自体も逆順になります。

これをVBAで行うには

列の内容を逆順にして
列ごとの順番を逆にする。

180度回転では行と列の入れ替えはしないで
逆順にするという操作で出来るんですね。

VBAでこれらの操作を行うのに
今回は選択した範囲を配列に格納して
For文とUBoundを使って入れ替えをしていきます。
セルの内容を配列に格納して使用する方法については
こちらの記事も参照してください。
セルの値を配列に格納して使う

 

コードはこちら

セルの内容を90度回転するコード:

Sub macro180211a()
'セルの内容を90度回転
'回転させたい範囲を選んでマクロ実行する
'回転後の内容はセルA1を基準 に出力

    Dim i As Integer, j As Integer
    Dim vData As Variant
   
    'Variant型の変数に選択範囲の値を入れる
    vData = Selection
   
    For i = 1 To UBound(vData, 1)
        For j = 1 To UBound(vData, 2)
            '行を逆順にして、行と列を入れ替える
            Cells(j, UBound(vData, 1) - (i - 1)) = vData(i, j)
        Next j
    Next i
       
End Sub


セルの内容を-90度回転するコード:

Sub macro180211b()
'セルの内容を-90度回転
'回転させたい範囲を選んでマクロ実行する
'回転後の内容はセルA1を基 準に出力

    Dim i As Integer, j As Integer
    Dim vData As Variant
   
    'Variant型の変数に選択範囲の値を入れる
    vData = Selection
   
    For i = 1 To UBound(vData, 1)
        For j = 1 To UBound(vData, 2)
            '行と列を入れ替えて列ごとの内容を逆順にする
            Cells(UBound(vData, 2) - (j - 1), i) = vData(i, j)
        Next j
    Next i
   
End Sub


セルの内容を180度回転するコード:

Sub macro180211c()
'セルの内容を180度回転
'回転させたい範囲を選んでマクロ実行
'回転後の内容はセルA1を基準に 出力

    Dim i As Integer, j As Integer
    Dim vData As Variant
   
    'Variant型の変数に選択範囲の値を入れる
    vData = Selection
   
    For i = 1 To UBound(vData, 1)
        For j = 1 To UBound(vData, 2)
            '列の内容を逆順にして列ごとの順番を逆にする
             Cells(UBound(vData, 1) - (i - 1), _
                UBound(vData, 2) - (j - 1)) = vData(i, j)
        Next j
    Next i
   
End Sub

ちなみに反転は、
行方向の反転なら行の内容を逆順に
列方向の反転なら列の内容を逆順にすればできそうです。
興味があれば上記のコードを改造して作ってみてください。

| | コメント (6) | トラックバック (0)

« 2018年1月 | トップページ | 2018年3月 »