2009-06-15 24 views

答えて

8

ANSI文字だけが必要な場合は、StrConv()関数as is done hereを使用できます。

+0

両方の回答がうまくいきますが、これはコーディングと時間が少なくて済みます。 – Ubalo

18

マシューはANSIに変換する方法を答えていますが、まだ元のUnicode文字列を表すために、結果のバイト配列を望んでいた場合、あなたは単にそれを直接割り当てると思います。これだけです

Public Sub Main() 
    Dim b() As Byte 
    Dim s As String 
    s = "Whatever" 
    b = s 'Assign Unicode string to bytes.' 
    s = b 'Works in reverse, too!' 
    Debug.Print s 
End Sub 

を。 16バイトのバイト配列で終わり、各連続したペアは1つのUnicode文字を記述します。

+1

あなたのコメントをVBコードサンプルで適切に強調表示するには、各コード行の最後に別のアポストロフィを追加してください – MarkJ

+1

ガイドはありますか?私は過度に退屈なコードを入力することを発見した。 –

-1
' a bit of an example 
' had some strings down column G 
' nothing in columns "F" or "H" so that current works 
' Think about it.. there are many many columns 
' so leave blank columns on each side of setsof dats 
' then currentregion works ... IFF no blank rows in the data 
' 
' problem to solve some text was Fred3 John2 Blue3 
' others were Bert 3 Green 2 ... which was the require format 
' the ASC char 1 ..255 are the odd or even 
' numbered bytes if array is 1 based or 0 based 
' 
Private Sub CommandButton1_Click() 
    Dim RV$, Ra As Range, Ri&, AL%, WSA() As Byte 
    Dim Ci%, WS$, LV As Byte 

    Set Ra = Range("g8").CurrentRegion 
    For Ri = 1 To Ra.Rows.Count 
     WSA = CStr(Ra(Ri, 1).value) 
     AL = UBound(WSA) 
     LV = WSA(AL - 1) ' last char byte value 
     If LV > 47 And LV < 58 Then ' 0 to 9 
      If WSA(AL - 3) <> 32 Then ' no space " " 
       ReDim Preserve WSA(AL + 2) ' allow 1 more char 
       WSA(AL - 3) = 32 ' put in space 
       WSA(AL - 1) = LV ' return char 
       WS = WSA ' back to a string 
       Ra(Ri, 1) = WS ' back to the cell 
      End If 
     End If 
    Next Ri 
End Sub 

' of course the normal VBAcommands Instr len Mid replace & 
' would do the job ... but my brain is lazy and needed some exercise 
関連する問題