Home > Excel VBA > Function Archive

Function Archive

エクセル データ変換関数(Excel Function to convert data)

20年前(1980年代)の初期のPascal言語では、今のようなStringやVariantなどの変数がなかったので、文字列を読み込む際は、1文字ずつループで読み込みをしていました。変数CharでスペースやIntegerを読み込もうとするとエラーとなり、条件を指定してから読み込むコードを書いていました。Turbo Pascalがパソコン用のソフトで出てきたころからStringが認識できるようになりました。

Excel では、セル値などの(.Value)の付いているものはほとんどがVariant型です(Spinbutton.Value=Long などの例外もあります)。エクセルの数式で、
Range("A1").Value+Range("B1").Value
などと、セル値同士を計算させることも出来ます。しかしVariant型同士の計算式はエクセルが自動で処理してくれているだけで、これはエラーの原因にもなります。簡単な処理であれば問題はないでしょうが、大量の計算処理をした場合は何の保証もありません。PascalであれExcelであれ、扱っているデータタイプをしっかりと区別してから処理させることは基本であり、とても重要なことです。

20 years ago (the second half of the 1980s), I have leaned Pascal language. In Pascal at that time, since there were no variables, such as present String and Variant, when reading a character string, it was reading one character at a time by the loop. The code read after specifying conditions to prevent error. When Turbo Pascal came out with the software for personal computers, Pacal could recognize Var String.

In Excel, most of variables, such as a cell value are Variant type (there is also an exception of Spinbutton.Value=Long etc.). Cell values can also be made to calculate with Range("A1").Value+Range("B1").Value etc. with the expression of Excel. However, this formula is calculated automatically by Excel. While it is in easy processing, it will not cause error. But when a lot of calculation processing is carried out, there is no guarantee. Whether if Pascal, Excel or other language, it is foundations to make it to process, after distinguishing the data type, and it is very important.

VBA FunctionData ConversionExample
CInt(Expression)Integer型に変換
Convert Numeric to Integer

CInt("32767")=32767
CInt("32768")=Error(オーバーフロー)
CInt("-32768")=-32768
CInt("-32769")=Error(オーバーフロー)

CLng(Expression)Long型に変換
Convert Numeric to Long

CLng("100")=100
-2,147,483,648~2,147,483,647の範囲(4Byte)
CLng("2,147,483,648")=Error(オーバーフロー)

CSng(Expression)Single型に変換
Convert Numeric to Single

CSng("0.123456")=0.123456
1.401298E-45 ~ 3.402823E38(プラス値)
-3.402823E38 ~ -1.401298E-45(マイナス値)
CSng("3.402824E38")=Error(オーバーフロー)
CSng(1.401298E-46)=0

CDbl(Expression)Double型に変換
Convert String or Numeric to Double

4.94065645841247E-324 ~ 1.79769313486231E+308(プラス値)
-1.79769313486231E308 ~ -4.94065645841247E-324(マイナス値)
CDbl("1.79769313486232E308")=>オーバーフロー
CDbl("1.79769313486231E308")=1.79769313486231E+308

CBool(Expression)Boolean型に変換
Convert String or Numeric to Boolean

CBool(0)=FALSE
CBool(0以外の数値)=TRUE
CBool(0.1)=TRUE
CBool(CInt(0.1))=FALSE
CBool("String")=Error(型が一致しない)
CBool("TRUE")=TRUE
CBool("0")=FALSE
CBool("2")=TRUE

CStr(Expression)String型に変換
Convert to String

CStr(10)="10"
CStr(CSng("3.402823E38"))="3.402823E+38"
CStr(CSng("3.402824E38"))=Error(文字列変換の前にオーバーフロー)

CVar(Expression)Variant型に変換
Convert to Variant

CVar(CStr(10) * CInt(19))=190
CVar(CStr(10) & CInt(19))=1019
特殊な関数で、使い方次第でエラーの原因になる可能性があります。

CDec(Expression)10進数型(Decimal)に変換
Convert String or Numeric to Decimal

CDec("1")=1
CDec("String")=Error(型が一致しない)
VBの計算は2進数が基準のため、精密な計算をする際に
少数点以下の誤差(以下の例)をなくすために使う
CDec(CSng(1.2345E-12))=0.00000012345
CSng(1.2345E-12)=0.00000012345000239

CByte(Expression)Byte型に変換
Convert String or Numeric to Byte

CByte("1")=1
CByte(255)=255
CByte(-0.5)=0
CByte(256)=Error(オーバーフロー)
CByte(-5.01)=Error(オーバーフロー)

CDate(Expression)Date型に変換
Convert String or Numeric to Date

CDate("平成20年4月29日")=2008/04/29
CDate("1")=1899/12/31
CDate(2)=1900/01/01
CDate(0)=0:00:00
CDate("10000")=1927/05/18

Hex(Expression)16進数に変換
Convert to Hexadecimal

Hex("&h5a")=5A
Hex("&08")=Error
Hex(1000)=3E8
CLng(Hex(1000))=300000000 (Notes:"3E8"→"3E+8")

Val(Expression)文字列を数値に変換
Convert String to Numeric

Val("1000")=1000
Val(1)=1
Val("1.23")=1.23
Val("1  0,12")=10
(スペースや"コンマなどのキャラクター文字以下"は無視される)
Val("全角")=0

Index of all entries

Home > Excel VBA > Function Archive

Return to page top