2017-02-07 4 views
1

私は最初のVBAサブを書いています。それはそれが想定していた方法で動作していますが、間違った部分を見つけ出すことはできません。 2バイトの日本語とラテン文字の文字列とスペースがある場合、2バイトのスペース、文字、数字、句読点を1バイトに選択的に変換することになっています。ダブルバイトからシングルバイト文字へのVBAの選択的変換

In this picture, the top row represents the input and the bottom row the desired output of spaces, letters, numbers, and punctuation converted to single-byte while the Japanese characters remain intact.

However, this is what is happening when I run the sub. Clearly it's working, but also something is off with my concatenation.

コードは以下であり、問​​題の全角文字に対応するUTF-16コードの範囲を「キャッチと変換」に基づいて動作します。ローカライズされたマシン(言語/地域が日本に設定されている場合)でのみ機能しますが、私のコードに関する問題はローカライズされた機能とは関係ありません。私が間違っていることについて助けがあれば、大いに喜ばれるでしょう!

Public Sub Converter() 
    Dim objRange As Range 
     For Each objRange In ActiveSheet.UsedRange 
     Call Alphanumeric(objRange) 
    Next 
End Sub 

Private Sub Alphanumeric(ByRef objRange As Range) 
    Dim strIn As String 
    Dim strOut As String 
    Dim strAlphanumeric As String 
    Dim i As Integer 

    If objRange.HasFormula Or _ 
     VarType(objRange.Value) <> vbString Then 
     Exit Sub 
    End If 

    strIn = objRange.Value 
    strOut = "" 
    strAlphanumeric = "" 

    For i = 1 To Len(strIn) 
     If AscW(Mid(strIn, i, 2)) + 65536 >= 65280 And _ 
      AscW(Mid(strIn, i, 2)) + 65536 <= 65370 Then 
      strAlphanumeric = strAlphanumeric & Mid(strIn, i, 1) 
     Else 
      If strAlphanumeric <> "" Then 
       strOut = strOut & StrConv(strIn, vbNarrow) 
       strAlphanumeric = "" 
      End If 
      strOut = strOut & Mid(strIn, i, 1) 
     End If 
    Next 

    objRange.Value = strOut 

End Sub 

答えて

0

私はライン

strOut = strOut & StrConv(strIn, vbNarrow)

はい、私の目に

strOut = strOut & StrConv(strAlphanumeric, vbNarrow) 
+1

であるべき!!疑いますそれがそれでした。どうもありがとうございます。 – mixadelic

関連する問題