2006年07月24日(月)
Excelで数字をタイ・英・日本語の文字列へ変換する関数 [タイの電脳事情 電脳街情報]
情報処理の実験でBAHTTEXTという関数が使われていました。いったいなんだろうと思って調べてみたら、ExcelやCalcなど表計算プログラムで数字をタイ語の文字列へ変換する関数だとわかりました。
すごい!タイの会社の経理課でいっぱい使われている関数の一つに違いありません。ただうちの経理システムとは無縁です。見積書や請求書などを発行する際、メインに使っているのは英語なので、むしろ数字を英語の文字列に変換する関数があったほうがありがたいと思っています。しかし、残念ながらこのような関数は標準的に存在していないようです。
では、手打ちするかと思っても、ちょっと諦めが早いような気がします。マクロを書けば、できないことはないでしょうが、なにげにGoogleで excel number to text spell function というキーワードを入れてみたら、マイクロソフトのヘルプページ(
http://support.microsoft.com/?kbid=213360
)
に辿り着きました。
このページによると、VBAでSPELLNUMBERという関数を作成すればいいそうです。
Excelが立ち上がっている状態でALT+F11でVBAを起動して、そのページにあるコードを登録すれば、SPELLNUMBER関数ができあがります。
デフォルトは数字をDollarsとCentsへ変換するので、バーツ、サターンにしたい場合、コード内のDollarsをBahtsへ、CentsをSatangsへ変更する必要があります。下記は修正したコードです。(プログラムを少しできる方ならご自分で修正できると思うので、下記のコードは要らないかもしれません。)
Function SpellNumber(ByVal MyNumber)
Dim Bahts, Satangs, Temp
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert Satangs and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Satangs = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
"00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Bahts = Temp & Place(Count) & Bahts
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Bahts
Case ""
Bahts = "No Bahts"
Case "One"
Bahts = "One Baht"
Case Else
Bahts = Bahts & " Bahts"
End Select
Select Case Satangs
Case ""
Satangs = " and No Satangs"
Case "One"
Satangs = " and One Satang"
Case Else
Satangs = " and " & Satangs & " Satangs"
End Select
SpellNumber = Bahts & Satangs
End Function
' Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If
' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function
' Converts a number from 10 to 99 into text.
Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
Select Case Val(TensText)
Case 10: Result = "Ten"
Case 11: Result = "Eleven"
Case 12: Result = "Twelve"
Case 13: Result = "Thirteen"
Case 14: Result = "Fourteen"
Case 15: Result = "Fifteen"
Case 16: Result = "Sixteen"
Case 17: Result = "Seventeen"
Case 18: Result = "Eighteen"
Case 19: Result = "Nineteen"
Case Else
End Select
Else ' If value between 20-99...
Select Case Val(Left(TensText, 1))
Case 2: Result = "Twenty "
Case 3: Result = "Thirty "
Case 4: Result = "Forty "
Case 5: Result = "Fifty "
Case 6: Result = "Sixty "
Case 7: Result = "Seventy "
Case 8: Result = "Eighty "
Case 9: Result = "Ninety "
Case Else
End Select
Result = Result & GetDigit _
(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function
' Converts a number from 1 to 9 into text.
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "One"
Case 2: GetDigit = "Two"
Case 3: GetDigit = "Three"
Case 4: GetDigit = "Four"
Case 5: GetDigit = "Five"
Case 6: GetDigit = "Six"
Case 7: GetDigit = "Seven"
Case 8: GetDigit = "Eight"
Case 9: GetDigit = "Nine"
Case Else: GetDigit = ""
End Select
End Function
Posted by ぷーちゃん at 12時54分
コメント
すごい!そんなこともエクセルでできるんですね(驚)!
退職したときに職安のやってる「エクセルエキスパート講座(だったかな?)」なるものを受講したことがありますが、関数、確かにやりました。もっと基本的なやつですけど。
しかしぷーちゃんって頭いいなぁ、って率直な感想です。理系とは縁遠い私の人生にはすべては理解できません。すごいなぁ、ぷーちゃん。
ん?関数がすごい→ぷーちゃんがすごい、という感想にかわってしまいました(笑)。
>なおちゃん
コメントありがとうございました。Excelの操作は1,2時間あれば誰でも理解できると思います。ポイントは関数の使い方やセルの絶対参照、相対参照の違いなどです。
それよりもっと難しいのは、問題を解くために、どうやって数式をたてればいいかということです。