全12回 Excel VBAコースの第11回の内容です。
罫線の引き方
'罫線を引く
Range("A1:C3").Borders.LineStyle = xlContinuous
'罫線を消す
Range("A1:C3").Borders.LineStyle = xlLineStyleNone
部分的に罫線を引きたい時は、Bordersで定数を指定する。
線の形状は、右辺で指定する。
Range("A1:C3").Borders(xlEdgeTop).LineStyle = xlContinuous '外枠の上線 → 実線
Range("A1:C3").Borders(xlEdgeBottom).LineStyle = xlDouble '外枠の下線 → 二重線
Range("A1:C3").Borders(xlEdgeRight).LineStyle = xlDash '外枠の右線 → 破線
Range("A1:C3").Borders(xlEdgeLeft).LineStyle = xlDot '外枠の左線 → 点線
Range("A1:C3").Borders(xlInsideVertical).LineStyle = xlDashDot '内側の縦線 → 一点鎖線
Range("A1:C3").Borders(xlInsideHorizontal).LineStyle = xlDashDotDot '内側の横線 → 二点鎖線
同じオブジェクトが連続するため省略したい時は、Withを使う。
With Range("A1:C3")
.Borders(xlEdgeTop).LineStyle = xlContinuous '外枠の上線 → 実線
.Borders(xlEdgeBottom).LineStyle = xlDouble '外枠の下線 → 二重線
.Borders(xlEdgeRight).LineStyle = xlDash '外枠の右線 → 破線
.Borders(xlEdgeLeft).LineStyle = xlDot '外枠の左線 → 点線
.Borders(xlInsideVertical).LineStyle = xlDashDot '内側の縦線 → 一点鎖線
.Borders(xlInsideHorizontal).LineStyle = xlDashDotDot '内側の横線 → 二点鎖線
End With
セルの背景色や文字色を変更する
●色を変更するプロパティ
・背景色を変更する⇒Interior
・文字色を変更する⇒Font
●色の指定方法
・ColorIndex(Excelが決めた色番号)
・Color(16進数またはRGB)
' 文字色を赤にする
Cells(1, 1).Font.ColorIndex = 3
' 背景色を黄色にする
Cells(1, 2).Interior.ColorIndex = 6
' 文字色を赤にする(16進数)
Cells(2, 1).Font.Color = &H0000FF
' 文字色を赤にする(RGB)
Cells(2, 2).Font.Color = RGB(255, 0, 0)
' 背景色を黄色にする(16進数)
Cells(5, 5).Interior.Color = &H00FFFF
' 背景色を黄色にする(RGB)
Cells(3, 2).Interior.Color = RGB(255, 255, 0)
【おまけ】クリアの種類
Clear ⇒セルの書式と値を削除
ClearContents ⇒セルの値を削除
ClearFormats ⇒セルの書式を削除
関数とは
プログラムの部品のようなものです。
何かを入れると、何かの処理をして、何かを出してくれます。
Excel VBAの場合、3種類の関数があります。
①VBA関数
②ワークシート関数
③Functionプロシージャを使って自分で作る関数
日付の扱い方
Date(日付)型
変数にはDate(日付)型があり、西暦100年1月1日 ~ 西暦9999年12月31日 の日付と時刻を表します。
現在日付や時刻を表す関数
Now 現在の日付+時刻を表示する
Date 現在の日付を表示する
Time 現在の時刻を表示する
Format関数
Format(値, 書式) と書くと、値を指定の書式に変換します。