『Excel』 VBAでちょっと便利!?(その17)

2019年06月12日 仕事のこだわり

Excelでのプログラミング17回目です。

セルのプロパティについての7回目の今回は、「HorizontalAlignment」と「VerticalAlignment」についてです。

■セル(Rangeオブジェクト)のプロパティ一覧(よく使われるものを抜粋)

◇ HorizontalAlignment 

HorizontalAlignmentは、セル内の値の表示位置(水平方向)を設定します。

◇ VerticalAlignment 

VerticalAlignmentは、セル内の値の表示位置(垂直方向)を指定します。

では、以下の表でそれぞれの文字の表示位置を設定してみます。

~~~~~~~~~~~~~~~~~~~~~~

Sub test()

‘左上

Cells(2, 2).HorizontalAlignment = xlLeft

Cells(2, 2).VerticalAlignment = xlTop

 

‘左中

Cells(3, 2).HorizontalAlignment = xlLeft

Cells(3, 2).VerticalAlignment = xlCenter

 

‘左下

Cells(4, 2).HorizontalAlignment = xlLeft

Cells(4, 2).VerticalAlignment = xlBottom

 

‘中上

Cells(2, 3).HorizontalAlignment = xlCenter

Cells(2, 3).VerticalAlignment = xlTop

 

‘中央

Cells(3, 3).HorizontalAlignment = xlCenter

Cells(3, 3).VerticalAlignment = xlCenter

 

‘中下

Cells(4, 3).HorizontalAlignment = xlCenter

Cells(4, 3).VerticalAlignment = xlBottom

 

‘右上

Cells(2, 4).HorizontalAlignment = xlRight

Cells(2, 4).VerticalAlignment = xlTop

 

‘右中

Cells(3, 4).HorizontalAlignment = xlRight

Cells(3, 4).VerticalAlignment = xlCenter

 

‘右下

Cells(4, 4).HorizontalAlignment = xlRight

Cells(4, 4).VerticalAlignment = xlBottom

 

End Sub

~~~~~~~~~~~~~~~~~~~~~~

◆実行結果◆

それぞれの位置が設定できました。

次回もセルのプロパティの続きです。

 

Copyright c syshan.jp all rights reserved.