文字操作 ひらがな・カタカナをローマ字(英字)変換
※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。
※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。
Dim strTwo(64, 2) As String, strOne(85, 2) As String Public Function RomajiConversion(ByVal HiraKata As String) As String '**************************************************** 'ひらがな・カタカナをローマ字(英字)変換 '**************************************************** '引数[HiraKata]を全てローマ字小文字(英字)変換 '引数[HiraKata]はひらがな・カタカナ何れも変換可能(※1) '注意 サブプロシージャー[ReadTable]が同じモジュール内に必要 '「なっとう」等の[っ]の場合、ローマ字特有の「na[tt]ou」[tt]を処理(※2) Dim CnvOne As String, CnvTwo As String Dim strTemporary As String Dim str As String Dim blnFlg As Boolean Dim cnt As Long Dim intMach As Integer ReadTable 'テーブル読込 '(※1)--------------------------------------------------------------------- 'vbHiragana : 文字列内のカタカナをひらがなに変換します。 'vbWide : 文字列内の半角文字 (1 バイト) を全角文字 (2 バイト) に変換します。 '既にひらがなの場合はそのまま変換しません。 HiraKata = StrConv(HiraKata, vbHiragana Or vbWide) '-------------------------------------------------------------------------- '初期設定 strTemporary = "" cnt = 1 blnFlg = False Do While cnt <= Len(HiraKata) '文字列全てを処理 CnvTwo = "" '初期設定 CnvOne = "" '初期設定 '注意 Do...Loop ステートメントは永久ループの危険ありの為、使用不可 '2文字の該当値検索 str = Mid(HiraKata, cnt, 2) '該当文字格納 For intMach = 1 To 64 If str = strTwo(intMach, 1) Then CnvTwo = strTwo(intMach, 2) Exit For '合致なら抜ける End If Next intMach '1文字の該当値検索 str = Mid(HiraKata, cnt, 1) '該当文字格納 For intMach = 1 To 85 If str = strOne(intMach, 1) Then CnvOne = strOne(intMach, 2) Exit For '合致なら抜ける End If Next intMach If CnvTwo <> "" Then '2文字変換完了の場合 If blnFlg Then 'フラグが該当する場合 strTemporary = strTemporary & Left(CnvTwo, 1) '2文字変換中1文字だけ余分に追加 End If strTemporary = strTemporary & CnvTwo '2文字追加 cnt = cnt + 2 '処理を2つ進める blnFlg = False '該当フラグを降ろす ElseIf CnvOne <> "" Then '1文字変換完了の場合 If blnFlg Then 'フラグが該当する場合 strTemporary = strTemporary & Left(CnvOne, 1) '1文字余分に追加 End If strTemporary = strTemporary & CnvOne '1文字追加 cnt = cnt + 1 '処理を1つ進める blnFlg = False '該当フラグを降ろす ElseIf Mid(HiraKata, cnt, 1) = "っ" Then '(※2)両方無変換の場合で該当文字が「っ」の場合 blnFlg = True '該当フラグを立てる cnt = cnt + 1 '処理を1つ進める Else strTemporary = strTemporary & Mid(HiraKata, cnt, 1) '全てに該当しない場合 cnt = cnt + 1 '処理を1つ進める End If Loop RomajiConversion = strTemporary End Function Private Sub ReadTable() '*********************************** 'RomajiConversion用テーブル '*********************************** '*ひらがな・カタカナをローマ字変換テーブル '「っ」はありません '「ん」は「n」にて変換、用途により「nn」変更してください。 '長音「ろーま」は「ro-ma」[-]で処理 strTwo(1, 1) = "きぃ": strTwo(1, 2) = "kyi" strTwo(2, 1) = "きぇ": strTwo(2, 2) = "kye" strTwo(3, 1) = "きゃ": strTwo(3, 2) = "kya" strTwo(4, 1) = "きゅ": strTwo(4, 2) = "kyu" strTwo(5, 1) = "きょ": strTwo(5, 2) = "kyo" strTwo(6, 1) = "ぎぃ": strTwo(6, 2) = "gyi" strTwo(7, 1) = "ぎぇ": strTwo(7, 2) = "gye" strTwo(8, 1) = "ぎゃ": strTwo(8, 2) = "gya" strTwo(9, 1) = "ぎゅ": strTwo(9, 2) = "gyu" strTwo(10, 1) = "ぎょ": strTwo(10, 2) = "gyo" strTwo(11, 1) = "しぃ": strTwo(11, 2) = "syi" strTwo(12, 1) = "しぇ": strTwo(12, 2) = "she" strTwo(13, 1) = "しゃ": strTwo(13, 2) = "sha" strTwo(14, 1) = "しゅ": strTwo(14, 2) = "shu" strTwo(15, 1) = "しょ": strTwo(15, 2) = "sho" strTwo(16, 1) = "じぃ": strTwo(16, 2) = "zyi" strTwo(17, 1) = "じぇ": strTwo(17, 2) = "je" strTwo(18, 1) = "じゃ": strTwo(18, 2) = "ja" strTwo(19, 1) = "じゅ": strTwo(19, 2) = "ju" strTwo(20, 1) = "じょ": strTwo(20, 2) = "jo" strTwo(21, 1) = "ちぃ": strTwo(21, 2) = "tyi" strTwo(22, 1) = "ちぇ": strTwo(22, 2) = "che" strTwo(23, 1) = "ちゃ": strTwo(23, 2) = "cha" strTwo(24, 1) = "ちゅ": strTwo(24, 2) = "chu" strTwo(25, 1) = "ちょ": strTwo(25, 2) = "cho" strTwo(26, 1) = "ぢぃ": strTwo(26, 2) = "dyi" strTwo(27, 1) = "ぢぇ": strTwo(27, 2) = "dye" strTwo(28, 1) = "ぢゃ": strTwo(28, 2) = "dya" strTwo(29, 1) = "ぢゅ": strTwo(29, 2) = "dyu" strTwo(30, 1) = "ぢょ": strTwo(30, 2) = "dyo" strTwo(31, 1) = "にぃ": strTwo(31, 2) = "nyi" strTwo(32, 1) = "にぇ": strTwo(32, 2) = "nye" strTwo(33, 1) = "にゃ": strTwo(33, 2) = "nya" strTwo(34, 1) = "にゅ": strTwo(34, 2) = "nyu" strTwo(35, 1) = "にょ": strTwo(35, 2) = "nyo" strTwo(36, 1) = "ひぃ": strTwo(36, 2) = "hyi" strTwo(37, 1) = "ひぇ": strTwo(37, 2) = "hye" strTwo(38, 1) = "ひゃ": strTwo(38, 2) = "hya" strTwo(39, 1) = "ひゅ": strTwo(39, 2) = "hyu" strTwo(40, 1) = "ひょ": strTwo(40, 2) = "hyo" strTwo(41, 1) = "びぃ": strTwo(41, 2) = "byi" strTwo(42, 1) = "びぇ": strTwo(42, 2) = "bye" strTwo(43, 1) = "びゃ": strTwo(43, 2) = "bya" strTwo(44, 1) = "びゅ": strTwo(44, 2) = "byu" strTwo(45, 1) = "びょ": strTwo(45, 2) = "byo" strTwo(46, 1) = "ぴぃ": strTwo(46, 2) = "pyi" strTwo(47, 1) = "ぴぇ": strTwo(47, 2) = "pye" strTwo(48, 1) = "ぴゃ": strTwo(48, 2) = "pya" strTwo(49, 1) = "ぴゅ": strTwo(49, 2) = "pyu" strTwo(50, 1) = "ぴょ": strTwo(50, 2) = "pyo" strTwo(51, 1) = "ふぁ": strTwo(51, 2) = "fa" strTwo(52, 1) = "ふぃ": strTwo(52, 2) = "fi" strTwo(53, 1) = "ふぇ": strTwo(53, 2) = "fe" strTwo(54, 1) = "ふぉ": strTwo(54, 2) = "fo" strTwo(55, 1) = "みぃ": strTwo(55, 2) = "myi" strTwo(56, 1) = "みぇ": strTwo(56, 2) = "mye" strTwo(57, 1) = "みゃ": strTwo(57, 2) = "mya" strTwo(58, 1) = "みゅ": strTwo(58, 2) = "myu" strTwo(59, 1) = "みょ": strTwo(59, 2) = "myo" strTwo(60, 1) = "りぃ": strTwo(60, 2) = "ryi" strTwo(61, 1) = "りぇ": strTwo(61, 2) = "rye" strTwo(62, 1) = "りゃ": strTwo(62, 2) = "rya" strTwo(63, 1) = "りゅ": strTwo(63, 2) = "ryu" strTwo(64, 1) = "りょ": strTwo(64, 2) = "ryo" strOne(1, 1) = "ー": strOne(1, 2) = "-" strOne(2, 1) = "ぁ": strOne(2, 2) = "xa" strOne(3, 1) = "あ": strOne(3, 2) = "a" strOne(4, 1) = "ぃ": strOne(4, 2) = "xi" strOne(5, 1) = "い": strOne(5, 2) = "i" strOne(6, 1) = "ぅ": strOne(6, 2) = "xu" strOne(7, 1) = "う": strOne(7, 2) = "u" strOne(8, 1) = "ぇ": strOne(8, 2) = "xe" strOne(9, 1) = "え": strOne(9, 2) = "e" strOne(10, 1) = "ぉ": strOne(10, 2) = "xo" strOne(11, 1) = "お": strOne(11, 2) = "o" strOne(12, 1) = "か": strOne(12, 2) = "ka" strOne(13, 1) = "が": strOne(13, 2) = "ga" strOne(14, 1) = "き": strOne(14, 2) = "ki" strOne(15, 1) = "ぎ": strOne(15, 2) = "gi" strOne(16, 1) = "く": strOne(16, 2) = "ku" strOne(17, 1) = "ぐ": strOne(17, 2) = "gu" strOne(18, 1) = "け": strOne(18, 2) = "ke" strOne(19, 1) = "げ": strOne(19, 2) = "ge" strOne(20, 1) = "こ": strOne(20, 2) = "ko" strOne(21, 1) = "ご": strOne(21, 2) = "go" strOne(22, 1) = "さ": strOne(22, 2) = "sa" strOne(23, 1) = "ざ": strOne(23, 2) = "za" strOne(24, 1) = "し": strOne(24, 2) = "shi" strOne(25, 1) = "じ": strOne(25, 2) = "ji" strOne(26, 1) = "す": strOne(26, 2) = "su" strOne(27, 1) = "ず": strOne(27, 2) = "zu" strOne(28, 1) = "せ": strOne(28, 2) = "se" strOne(29, 1) = "ぜ": strOne(29, 2) = "ze" strOne(30, 1) = "そ": strOne(30, 2) = "so" strOne(31, 1) = "ぞ": strOne(31, 2) = "zo" strOne(32, 1) = "た": strOne(32, 2) = "ta" strOne(33, 1) = "だ": strOne(33, 2) = "da" strOne(34, 1) = "ち": strOne(34, 2) = "chi" strOne(35, 1) = "ぢ": strOne(35, 2) = "di" strOne(36, 1) = "つ": strOne(36, 2) = "tsu" strOne(37, 1) = "づ": strOne(37, 2) = "du" strOne(38, 1) = "て": strOne(38, 2) = "te" strOne(39, 1) = "で": strOne(39, 2) = "de" strOne(40, 1) = "と": strOne(40, 2) = "to" strOne(41, 1) = "ど": strOne(41, 2) = "do" strOne(42, 1) = "な": strOne(42, 2) = "na" strOne(43, 1) = "に": strOne(43, 2) = "ni" strOne(44, 1) = "ぬ": strOne(44, 2) = "nu" strOne(45, 1) = "ね": strOne(45, 2) = "ne" strOne(46, 1) = "の": strOne(46, 2) = "no" strOne(47, 1) = "は": strOne(47, 2) = "ha" strOne(48, 1) = "ば": strOne(48, 2) = "ba" strOne(49, 1) = "ぱ": strOne(49, 2) = "pa" strOne(50, 1) = "ひ": strOne(50, 2) = "hi" strOne(51, 1) = "び": strOne(51, 2) = "bi" strOne(52, 1) = "ぴ": strOne(52, 2) = "pi" strOne(53, 1) = "ふ": strOne(53, 2) = "fu" strOne(54, 1) = "ぶ": strOne(54, 2) = "bu" strOne(55, 1) = "ぷ": strOne(55, 2) = "pu" strOne(56, 1) = "へ": strOne(56, 2) = "he" strOne(57, 1) = "べ": strOne(57, 2) = "be" strOne(58, 1) = "ぺ": strOne(58, 2) = "pe" strOne(59, 1) = "ほ": strOne(59, 2) = "ho" strOne(60, 1) = "ぼ": strOne(60, 2) = "bo" strOne(61, 1) = "ぽ": strOne(61, 2) = "po" strOne(62, 1) = "ま": strOne(62, 2) = "ma" strOne(63, 1) = "み": strOne(63, 2) = "mi" strOne(64, 1) = "む": strOne(64, 2) = "mu" strOne(65, 1) = "め": strOne(65, 2) = "me" strOne(66, 1) = "も": strOne(66, 2) = "mo" strOne(67, 1) = "ゃ": strOne(67, 2) = "xya" strOne(68, 1) = "や": strOne(68, 2) = "ya" strOne(69, 1) = "ゅ": strOne(69, 2) = "xyu" strOne(70, 1) = "ゆ": strOne(70, 2) = "yu" strOne(71, 1) = "ょ": strOne(71, 2) = "xyo" strOne(72, 1) = "よ": strOne(72, 2) = "yo" strOne(73, 1) = "ら": strOne(73, 2) = "ra" strOne(74, 1) = "り": strOne(74, 2) = "ri" strOne(75, 1) = "る": strOne(75, 2) = "ru" strOne(76, 1) = "れ": strOne(76, 2) = "re" strOne(77, 1) = "ろ": strOne(77, 2) = "ro" strOne(78, 1) = "わ": strOne(78, 2) = "wa" strOne(79, 1) = "ゐ": strOne(79, 2) = "wi" strOne(80, 1) = "ゑ": strOne(80, 2) = "we" strOne(81, 1) = "を": strOne(81, 2) = "wo" strOne(82, 1) = "ん": strOne(82, 2) = "n" strOne(83, 1) = "ゑ": strOne(83, 2) = "we" strOne(84, 1) = "を": strOne(84, 2) = "wo" strOne(85, 1) = "ん": strOne(85, 2) = "n" End Sub Private Sub test() MsgBox RomajiConversion("ろーま") MsgBox RomajiConversion("ハムレット") End Sub |