LoginSignup
75
93

More than 5 years have passed since last update.

Excel VBA チートシート

Posted at

たまにExcelでVBAを書いてて、毎回調べているようなことをメモ。

基本文法

メッセージ表示

MsgBox "Hello world"
Debug.Print "Hello world"

コメント

'コメント

変数宣言

Dim flag As Boolean
Dim num As Integer
Dim value As Double
Dim today As Date
Dim str As String

インクリメント/デクリメント

i = i + 1
i = i - 1

文字結合

join1 = "aaa" & "bbb"

制御文

If a = 0 Then

Elseif a = 1 Then

Else 

End If
i = 0
Do While i < 5

   If i = 3 Then
      Exit Do
   End If

   i = i + 1
Loop
For i = 0 To 4

  If i <> "" Then
     Exit For
  End If

Next

セルの操作

参照

a = Cells(1, 1).Value

代入

Cells(1, 1).Value = "Hoge"

シート指定

Worksheets("Sheet1").Cells(1, 1).Value = "Hoge"

Withで一括指定

With Cells(3, 6)
        .Value = "項目"
        .Font.Bold = True
        .Interior.ColorIndex = 15

シートの操作

アクティブにする

Worksheets("Sheet1").Activate

範囲指定

Range(Cells(1,1), Cells(2,2)).Borders.LineStyle = xIContinuous

Withで一括設定

With Range(Cells(1,1), Cells(2,2))
        .Borders.LineStyle = xIContinuous
        .Borders.ColorIndex = xlAutomatic
        .Columns.AutoFit

選択範囲の取得とセルの結合

選択した範囲で、横方向のみセルを結合するスクリプト。

Start = Selection(1).Column
End   = Selection(Selection.Count).Column
For i = Selection(1).Row To Selection(Selection.Count).Row
    Range(Cells(i, Start), Cells(i, End)).MergeCells = True
Next i

参考

VBA基礎文法最速マスター
http://d.hatena.ne.jp/nattou_curry_2/20100129/1264787849

75
93
1

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
75
93