ユーザー定義定数(Const ステートメント)
定数を使う
ユーザー定義の定数を使用するには、Const ステートメントを使って、定数の宣言をします。
[ Public | Private ] Const constname [ As type ] = expression
引数・戻り値
- constname ・・・ 定義する定数の名前を指定します(省略不可)。
- type ・・・ 定数のデータ型を指定します(省略可)。
- expression ・・・ 格納する値を指定します(省略不可)。
キーワード Public を指定するとその定数は、すべてのモジュールのどのプロシージャからも参照できます。一方、キーワード Private を指定すると宣言が行われたモジュール内でしか参照できなくなります。Public、Private を省略した場合、Private を指定したものとみなされます。
Const ステートメント 使用例
サンプルVBAソース
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
'消費税率 Const CTAXRATE As Double = 0.8 '消費税込みの金額を求める Function GetTaxIncludedPrice(myprice As Currency) As Currency GetTaxIncludedPrice = myprice * (1 + CTAXRATE) End Function '消費税を求める Function GetConsumptionTax(myprice As Currency) As Currency GetConsumptionTax = myprice * CTAXRATE End Function Sub Sample_Const() MsgBox "消費税:" & GetConsumptionTax(10000) & vbCrLf & _ "税込金額:" & GetTaxIncludedPrice(10000) End Sub |
実行結果
関連記事
-
-
四捨五入・切り上げ・切り捨て・丸め(Round 関数 他)
Round 関数(丸め)の使い方 【書式】 value = Round ( ex …
-
-
日付時刻:年・月・日を取得(Year、Month、Day 関数)
日付から年・月・日を取得する関数 【書式】 date_year = Year ( …
-
-
指定した値を8進数で表記した文字列に変換する(Oct 関数)
Oct 関数 の使い方 【書式】 result = Oct ( expressi …
-
-
オブジェクトの書式をクリアする(ClearFormats メソッド)
ClearFormats メソッド 対象となるオブジェクトの書式(スタイル)を初 …
-
-
ポイント単位の値に変換(CentimetersToPoints,InchesToPoints)
CentimetersToPoints メソッド センチメートル単位の値をポイン …
-
-
XlThemeColor 列挙型
XlThemeColor 列挙 定数 値 内容 xlThemeColorDark …
-
-
変数が数値型かどうかをチェックする( IsNumeric 関数 )
IsNumeric 関数の使い方 【書式】 result = IsNumeric …
-
-
配列の添字の最小値を「1」に設定する
Option Base ステートメントを使用する 【書式】 Option Bas …
-
-
文字列:文字列左から指定文字数取得(Left 関数,LeftB 関数)
Left 関数・LeftB 関数 の使い方 【書式】 result_left = …
-
-
日付時刻:現在の日付・時刻を取得・設定(Now、Date、Time)
現在の日付・時刻を取得・設定 現在の日付・時刻を取得(Now 関数、Date 関 …
