このページを元に作成
データ整理
データの整理で一番利用するのが、Excel。
漢字⇒ひらがな変換
ゲームのデータ検索システムを作成する際、様々なまとめサイトからデータを取得するが、 そこで困るのが、サイト毎の各種名称における差異。
正しい名称は何か?各サイトの差分を抽出し、1つにまとめたい。 など、ちょっと困ってしまう。 下記の関数を標準モジュールに登録し、セルにユーザー定義関数として埋め込みを利用する。
' -----------------------------------------------------------------------
' 漢字変換(漢字⇒カタカナ、ひらがな⇒ひらがな)
' [1] targetStr : 対象文字列
' [2] phoneticCount : 変換回数(何回目の候補を返すか)
'
' Return: 変換後文字列
' -----------------------------------------------------------------------
Function LIBSTF_Phonetics(ByRef targetStr As String, Optional phoneticCount As Integer = 0) As String
Dim i As Integer ' 変換回数
Dim convStr As String ' 変換後文字列
' 変換
convStr = Application.GetPhonetic(targetStr)
If convStr = "" Or Not targetStr Like "*[一-龝]*" Then
' 変換失敗、又は、Unicode(JIS は[亜-熙])
LIBSTF_Phonetics = targetStr
Exit Function
End If
' 変換回数に到達、又は、変換出来なくなるまで
i = 0
Do Until phoneticCount = i Or Application.GetPhonetic(targetStr) = ""
convStr = Application.GetPhonetic("")
i = i + 1
Loop
LIBSTF_Phonetics = convStr
End Function
カタカナ⇒ひらがな変換
上記の「漢字⇒ひらがな」変換では、カナなど変換されないため、更に関数を追加。 Excel関数に StrConv() は存在しないんだよね・・・単純な Wrapper 関数。
' -----------------------------------------------------------------------
' VBA StrConv
' [1] targetStr : 対象文字列
' [2] param : 変換種類
' : 1:Upper/2:Lower/3:単語の先頭大文字/4:半角⇒全角/8:全角⇒半角
' : 16:ひらがな⇒カタカナ/32:カタカナ⇒ひらがな
'
' Return: 変換後文字列
' -----------------------------------------------------------------------
Public Function LIBSTF_StrConv(ByRef targetStr As String, ByRef param As VbStrConv) As String
' Debug.Print VbStrConv.vbHiragana ' 32
' Debug.Print VbStrConv.vbKatakana ' 16
LIBSTF_StrConv = StrConv(targetStr, param)
End Function
参考リンク
- [[]]
- url
このwikiの更新情報RSS