sironekotoroの日記

Perl で楽をしたい

Excel VBAスタンダード 公式テキスト 7章 関数

7-1 配列を操作する関数

  • LBound 引数に指定した配列で使用できる最も小さなインデックス番号を返す
  • UBound 引数に指定した配列で使用できる最も大きなインデックス番号を返す
  • split 文字列データを区切り文字で区切る。
    • tmp = Split("tanaka/suzuki/yamada", "/")
    • perlsplitとは引数の順番が逆

7-2 データを判定する関数

  • IsArray 引数が配列のときにTrueを返す
Dim Files As Variant
' GetOpenFilename ファイルを開くダイアログボックスの表示
' MultiSelectにTrueで複数のファイルを参照できる
Files = Application.GetOpenFilename(MultiSelect:=True)
If Not IsArray(Files) Then MsgBox "キャンセルされました"
  • IsDate 引数が日付のときにTrueを返す
Dim buf As String
buf = InputBox("生年月日は?") '
If IsDate(buf) Then
    MsgBox Format(buf, "ggge年です")
Else
    MsgBox "日付を入れてください"
End If
  • IsNumeric 引数が数値のときにTrueを返す
Dim buf As String
buf = InputBox("税抜き金額は?")
If IsNumeric(buf) Then
' 2014年現在、消費税は8%
    MsgBox Format(buf * 1.08, "#,##0円(税込)")
Else
    MsgBox "数値を入力してください"
End If

7-3 日付を操作する関数

  • 'DateSerial' 引数で指定した年月日に該当する日付データを返す
Dim buf As String
buf = InputBox("判定する年は?")
If Day(DateSerial(buf, 3, 1) - 1) = 29 Then
' 閏年は"Leap Year"というそうな
    MsgBox "Leap Year"
Else
    MsgBox "Normal Year"
End If
  • ちょっとだけ手を加えてみる
Dim buf As String, yearcheck As String
buf = InputBox("判定する年は?")
If Day(DateSerial(buf, 3, 1) - 1) = 29 Then
' 閏年は"Leap Year"というそうな
    yearcheck = "Leap Year!"
Else
    yearcheck = "Normal Year"
End If

MsgBox yearcheck, Title:="LEAP YEAR?"