Visual Basic関数

Visual Basicの組み込み関数一覧の一覧とその使い方をご紹介します。

目次

  1. DateAdd
  2. DateSerial
  3. Format
  4. InStr
  5. IsDate
  6. IsEmpty
  7. IsNumeric
  8. LBound
  9. Mid
  10. MsgBox
  11. Randomize
  12. Rnd
  13. Trim
  14. Split
  15. Open
  16. UBound

DateAdd

Public Overloads Function DateAdd(
  ByVal Interval As DateInterval,
  ByVal Number As Double,
  ByVal DateValue As DateTime
) As DateTime
Public Overloads Function DateAdd(
  ByVal Interval As String,
  ByVal Number As Double,
  ByVal DateValue As Object
) As DateTime

DateAdd関数の使用例を次に示す。

Dim x As Variant

' 2012年11月23日の1か月前
x = DateAdd("m", -1, "2012.11.23")

DateSerial

Public Function DateSerial(
  ByVal Year As Integer,
  ByVal Month As Integer,
  ByVal Day As Integer
) As DateTime

指定した年月日に対応する日付(Date)型の値を返す。

DateSerial関数の使用例を次に示す。

Dim x As Date

x = DateSerial(2012, 11, 23)

Format

Function Format(
  expression As Object,
  style As String
) As String

InStr

Function InStr(
  string1 As String,
  string2 As String
) As Integer

IsDate

Public Function IsDate(ByVal Expression As Object) As Boolean

引数 expression で指定した値が日付型であればTrue、日付型でなければFalseを返す。

IsEmpty

IsEmpty(expression)

引数 expression で指定したバリアント型変数がEmpty値であればTrue、Empty値でなければFalseを返す。

IsNumeric

Public Function IsNumeric(ByVal Expression As Object) As Boolean

引数 expression で指定した値が数値型であればTrue、数値型でなければFalseを返す。

LBound

Public Function LBound(
  ByVal array As System.Array,
  Optional ByVal rank As Integer = 1
) As Integer
LBound関数の引数
Parameter Description
array 対象となる配列
rank 添え字の最大値を取得する配列の次元

配列の要素が1つの場合、LBound関数は0を返す。要素がない場合、LBound関数は-1を返す。

Mid

Function Mid(
  str As String,
  start As Integer,
  length As Integer
) As String

MsgBox

Public Function MsgBox(
  ByVal Prompt As Object,
  Optional ByVal Buttons As MsgBoxStyle = MsbBoxStyle.OKOnly,
  Optional ByVal Title As Object = Nothing
) As MsgBoxResult

MsgBox関数のパラメータを次に示す。

Prompt

ダイアログボックス内にメッセージとして表示する文字列(Sring)式を指定する。

Buttons

省略可能である。表示されるボタンの種類、個数、使用するアイコンのスタイル、標準ボタン及びメッセージボックスがモーダルかどうかなど、それらを表す値の合計値を指定する。Buttonsを表略すると、規定値は0になる。

Title

省略可能である。ダイアログボックスのタイトルバーに表示する文字列(String)式を指定する。Titleを省略すると、タイトルバーにはアプリケーション名が表示される。

Randomize

Public Shared Sub Randomize([Number])

RandomizeはNumberで指定した値をシード値として、Rnd関数の乱数ジェネレータを初期化する。Numberを省略した場合は、システムタイマから取得した値がシード値として使われる。

Rnd

Public Shared Function Rnd[(NUmber)] As Single

Rnd関数は、0以上、1未満の乱数を返す。

Rnd関数の使用例を次に示す。

Dim x As Single

Const lowerBound As Single = 1
Const upperBound As Single = 100

x = (upperBound - lowerBound) * Rnd + lowerBound

Trim

引数に指定した文字列の前後にあるスペース(空白)を除いた文字列を取得する。

Function Trim(str As String) As String
str = Trim("  string  ")

Split

Split関数は、文字列を区切って配列として返す。

Function Split(
  ByVal expression As String,
  Optional ByVal delimiter As String = " "
  Optional ByVal limit As Integer = -1,
  Optional ByVal compare As CompareMethod = CompareMethod.Binary
) As String()

stringには対象となる文字列を指定する。

separatorには区切り文字を指定する。

arr = Split("foo,bar,baz", ",")
For i = LBound(arr) To UBound(arr)
  Debug.Pring arr(i)
Next

Open

Open関数は、ファイルをオープンする。

Dim fileno As Integer
Dim buf

fileno = FreeFile
Open "C:\foo\bar.txt" For Input As #fileno

Do Until EOF(fileno)
  Line Input #fileno, buf
  Debug.Print buf
Loop

Close #fileno

UBound

Public Function UBound(
  ByVal array As System.Array,
  Optional ByVal rank As Integer = 1
) As Integer
UBound関数の引数
Parameter Description
array 対象となる配列
rank 添え字の最大値を取得する配列の次元

配列の要素が1つの場合、UBound関数は0を返す。要素がない場合、UBound関数は-1を返す。