Vorsorge

雑記帳

【vba】【法令用】選択範囲の漢数字を算用数字に変換(メモ)

Sub eGov用()
'
' 選択範囲の漢数字を英数字に置換
'

    Dim a_漢数字(0 To 11) As String
    
    a_漢数字(0) = ""
    a_漢数字(1) = "一"
    a_漢数字(2) = "二"
    a_漢数字(3) = "三"
    a_漢数字(4) = "四"
    a_漢数字(5) = "五"
    a_漢数字(6) = "六"
    a_漢数字(7) = "七"
    a_漢数字(8) = "八"
    a_漢数字(9) = "九"
    a_漢数字(10) = "十"
    a_漢数字(11) = "百"

    '初期化
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
        
    For i = 0 To 9
        For j = 0 To 9
            For k = 0 To 9
                With Selection.Find
                    '一桁
                    If i = 0 And j = 0 And (Not k = 0) Then
                        .Text = "([第法条項号前])" & a_漢数字(j) & a_漢数字(k) & "([条項号])"
                    '10~19
                    ElseIf i = 0 And j = 1 Then
                        .Text = "([第法条項号前])十" & a_漢数字(k) & "([条項号])"
                        
                    '20~99
                    ElseIf i = 0 Then
                        .Text = "([第法条項号前])" & a_漢数字(j) & "十" & a_漢数字(k) & "([条項号])"
                    '100~109
                    ElseIf i = 1 And j = 0 Then
                        .Text = "([第法条項号前])百" & a_漢数字(k) & "([条項号])"
                    '110~119
                    ElseIf i = 1 And j = 1 Then
                        .Text = "([第法条項号前])百十" & a_漢数字(k) & "([条項号])"
                    '120~199
                    ElseIf i = 1 Then
                        .Text = "([第法条項号前])百" & a_漢数字(j) & "十" & a_漢数字(k) & "([条項号])"
                    '200~209、300~309……
                    ElseIf j = 0 Then
                        .Text = "([第法条項号前])" & a_漢数字(i) & "百" & a_漢数字(k) & "([条項号])"
                    Else
                        .Text = "([第法条項号前])" & a_漢数字(i) & "百" & a_漢数字(j) & "十" & a_漢数字(k) & "([条項号])"

                    End If
                    
                    If i = 0 And j = 0 And (Not k = 0) Then
                        .Replacement.Text = "\1" & k & "\2"
                    ElseIf i = 0 Then
                        .Replacement.Text = "\1" & j & k & "\2"
                    '100以上
                    ElseIf 0 < i Then
                        .Replacement.Text = "\1" & i & j & k & "\2"
                    End If
                    
                    .Forward = True
                    .Wrap = wdFindStop
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchByte = False
                    .MatchAllWordForms = False
                    .MatchSoundsLike = False
                    .MatchFuzzy = False
                    .MatchWildcards = True
                End With
                
                Selection.Find.Execute Replace:=wdReplaceAll
            Next
        Next
    Next
           
    For i = 6 To 0 Step -1
        For j = 9 To 0 Step -1
            With Selection.Find
                '一桁
                If i = 0 Then
                    .Text = "([治正和成])" & a_漢数字(i) & a_漢数字(j) & "年"
                '10~19
                ElseIf i = 1 Then
                    .Text = "([治正和成])十" & a_漢数字(j) & "年"
                    
                '20~99
                Else
                    .Text = "([治正和成])" & a_漢数字(i) & "十" & a_漢数字(j) & "年"
                End If
                
                If j = 0 Then
                    .Replacement.Text = "\1" & j & "年"
                Else
                    .Replacement.Text = "\1" & i & j & "年"
                End If
                
                .Forward = True
                .Wrap = wdFindStop
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchByte = False
                .MatchAllWordForms = False
                .MatchSoundsLike = False
                .MatchFuzzy = False
                .MatchWildcards = True
            End With
            
            Selection.Find.Execute Replace:=wdReplaceAll
        Next
    Next
    
    For i = 9 To 0 Step -1
        For j = 9 To 0 Step -1
            With Selection.Find
                '一桁
                If i = 0 Then
                    If j = 0 Then
                    Else
                        .Text = "の" & a_漢数字(j)
                    End If
                '10~19
                ElseIf i = 1 Then
                    .Text = "の十" & a_漢数字(j)
                    
                '20~99
                Else
                    .Text = "の" & a_漢数字(i) & "十" & a_漢数字(j)
                End If
                
                If i = 0 Then
                    .Replacement.Text = "の" & j
                Else
                    .Replacement.Text = "の" & i & j
                End If
                
                .Forward = True
                .Wrap = wdFindStop
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchByte = False
                .MatchAllWordForms = False
                .MatchSoundsLike = False
                .MatchFuzzy = False
                .MatchWildcards = True
            End With
            
            Selection.Find.Execute Replace:=wdReplaceAll
        Next
    Next
End Sub