EXCEL VBAのメモ

基本構文

変数 = オブジェクト.プロパティ

ex. cellColor = Cells(1,1).Interior.color

オブジェクト.プロパティ = プロパティの値

ex. Cell(1,1).Interior.color = RGB( 240, 200, 100)

オブジェクト.メソッド

ex. Engine.Start

ex. Worksheets(1).Delete

オブジェクト.メソッド引数

ex. Worksheets.Add Before := Worksheets(2)

PlantUML Syntax:<br />
[Excel] — [book]<br />
[book] — [sheet(1)]<br />
[book] — [sheet(2)]<br />
[sheet(1)] — [sheet(1).cell(1,1)]<br />
[sheet(1)] — [sheet(1).cell(1,2)]<br />
[sheet(2)] — [sheet(2).cell(1,1)]<br />
[sheet(2)] — [sheet(2).cell(1,2)]<br />
” usemap=”#plantuml_map”></p>



<h2 class=変数

Dim 変数名 As 型

ex. Dim myFileName As Sting

データ型値の範囲
バイト型(Byte)0~255の正の整数を保存する
ブール型(Boolen)TrueまたはFalseを保存する
整数型(Integer)-32,768~32,767の整数値を保存する
(長)整数型(Long)Integerでは保存できない大きな桁の整数を保存する
-2,147,483,648~2,147,483,647
通貨型(Currency)Longよりも桁の大きな小数点を含む数値を保存する。
-922,337,203,685,477.5808~922,337,203,685,477.5807
(単精度浮動)小数点整数型(Single)少数点を含む数値を保存する。
正の値 1.4×10^-45~1.8×10^38
負の値 -3.4×10^38~-1.4×10^-45
(倍制度浮動)小数点数型(Double)Singleよりも桁の大きな少数点を含む数値を保存する。
正の値 4.9×10^-324~1.8×10^308
負の値 -1.8×10^308~-4.0×10^-324
日付型(Date)日付と時刻を格納する
文字列型(String)文字列を格納する
オブジェクト型(Object)オブジェクトへの参照を格納する
バリアント型(Variant)いろんな種類の値を格納する

ファイル操作

変数 = Dir( パス名 )

ex.
files = Dir( "c:\dir\*.xlsx")
Do While( files <> "" )
    wb = Workbooks.Open(files)
    files = Dir
Loop

制御分

If … Then … Else ステートメント

PlantUML Syntax:<br />
!pragma useVerticalIf on<br />
start<br />
if (if\nValue = 1) then (1)<br />
:then\nMsgBox “1”;<br />
elseif (elseif\nValue = 2) then (2)<br />
:MsgBox “2”;<br />
elseif (elseif\nValue = 3) then (3)<br />
:MsgBox “3”;<br />
else (other)<br />
:else\nMsgBox “Error No.”;<br />
endif<br />
stop<br />
” usemap=”#plantuml_map”></p>
</div>



<div class=

Sub SampleIf()
  If Cells(1,1).Value = 1 Then
    MsgBox "1"
  ElseIf Cells(1,1).Value = 2 Then
    MsgBox "2"
  ElseIf Cells(1,1).Value = 3 Then
    MsgBox "3"
  Else
    MsgBox "Error No."
  End If
End Sub


投稿日

カテゴリー:

投稿者:

タグ: