半角カンマ区切りの文字列を配列に代入する

例えばCSVファイルなどの項目別の分解などに使います。

Sub test()
     Dim line As String
     Dim barabara() As String
     line = "花,鳥,風,月"
     barabara() = Split(line, ",")
     Debug.Print "最初の要素は" + barabara(0)
     Debug.Print "2番目の要素は" + barabara(1)
     Debug.Print "3番目の要素は" + barabara(2)
     Debug.Print "4番目の要素は" + barabara(3)
     Debug.Print "最初の要素番号は"; LBound(barabara)
     Debug.Print "最後の要素番号は"; UBound(barabara)
     Debug.Print "要素数は"; UBound(barabara) - LBound(barabara()) + 1
 End Sub

これを実行すると、

最初の要素は花
2番目の要素は鳥
3番目の要素は風
4番目の要素は月
最初の要素番号は 0 
最後の要素番号は 3 
要素数は 4 

となります。 区切り文字を変える場合、split()関数の2つめのパラメータを変えればOKです。
VBAのお勉強 まとめへ戻る

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2011 simple blog いろいろ勉強中 Suffusion theme by Sayontan Sinha