0
1

More than 3 years have passed since last update.

VBAの基本操作(セル)

Last updated at Posted at 2021-04-16

VBAの初心者向けに基本操作方法(セル)を紹介していきたいと思います。

セルに文字列の表示
Cells(1, 1) = "こんにちは"
Range("A1") = "こんにちは"

Cells( )とRange( )はセルを指定します。

セル範囲を指定して文字列の表示
Range("A1", "A2") = "こんにちは"
Range(Cells(1, 1), Cells(1, 2)) = "こんにちは"

上記の様にRange( )とSells( )を組み合わせて使用することも出来ます。

セルの色変更
Cells(1, 1).Interior.Color = RGB(250, 0, 0)

RGBとは、色の表現法の一種で、赤 (Red)(●)、緑 (Green)(●)、青 (Blue)(●) の三つの原色を混ぜて幅広い色を再現する加法混合の一種です。表現方法については下記のサイトを参照すると良いです。
https://www.scollabo.com/banban/lectur/websafe.html

数列の表示形式
Cells(1, 1).NumberFormatLocal = "yyyy-mm-dd"  '20210416 ▶︎ 2021-04-16
Cells(1, 1).NumberFormatLocal = "#,###.##"    '12345678 ▶︎ 12,345,678.00
セルの削除
Cells(1, 1).Delete xlShiftToLeft   '削除して左へシフト
Cells(1, 1).Delete xlShiftToUp     '削除して上へシフト
Cells(1, 1).EntireRow.Delete       '行の削除
Cells(1, 1).EntireColumn.Delete    '#列の削除
セル内の文字列の削除
Cells(1, 1).ClearContents
行の挿入
Rows(1).Insert
コピーと貼り付け
Cells(1, 1).Copy            'コピーする
Cells(1, 2).PasteSpecial    '貼り付ける
切り取りと貼り付け
Cells(1, 1).Cut Destination:=Cells(1, 2)
枠線の操作
'上に枠線をつける
Range("B2").Borders(xlEdgeTop).LineStyle = xlContinuous
'範囲内の水平方向に線をつける
Range("B2", "D6").Borders(xlInsideHorizontal).LineStyle = xlContinuous
'範囲内の縦方向に線をつける
Range("B2", "D6").Borders(xlInsideVertical).LineStyle = xlContinuous

'枠線の上だけ削除
Range("B2").Borders(xlEdgeTop).LineStyle = xlLineStyleNone
'枠線を全て削除
Range("B2").Borders.LineStyle = xlLineStyleNone

 今回紹介した内容は普段からExcellを使用している方なら頻繁に利用する機能だと思います。これだけだとわざわざマクロを組む必要性はないと思いますが、いろいろな関数と組み合わせることでデータを集めたり拡散できたりといろいろなことができるようになります。

蛇足

ユーザーの操作無効化

Enabled関数とLocked関数を使用するとユーザー操作をできなくすることができます。

書き方
txtID.Enabled = False 'ロックしない場合はTrue
txtID.Locked = True   'ロックしない場合はFalse
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1