VBA 的全角半角转换解决方案(中文系统有效)

‘全角转半角

Function toBanJiao(str As String) As String

    Dim chA As String
    Dim i1 As Long
    Dim iAsc As Long
    
    For i1 = 1 To Len(str)
        chA = Mid(str, i1, 1)
        iAsc = CLng(Asc(chA)) + 23680
        If iAsc < 127 And iAsc > 31 Then
            toBanJiao = toBanJiao & Chr(iAsc)
        Else
            toBanJiao = toBanJiao & chA
        End If
    Next
    
End Function

’半角转全角
Function toQuanJiao(str As String) As String
    Dim chA As String
    Dim i1 As Long
    Dim iAsc As Long
        
    For i1 = 1 To Len(str)
        chA = Mid(str, i1, 1)
        iAsc = Asc(chA)
        If iAsc < 127 And iAsc > 31 Then
            toQuanJiao = toQuanJiao & Chr(iAsc - 23680)
        Else
            toQuanJiao = toQuanJiao & chA
        End If
    Next
        
End Function

你可能感兴趣的:(VBA)