Excel2000 VBA カンマ区切り文字を展開する [Excel2000 VBA独習]
Excel2000 VBA カンマ区切り文字を展開する
利用方法
文字列(例 東京都港区台場1-1-1 ○○TV)を手動で(東京都,港区,台場1-1-, ○○TV)のようにカンマを入れて、マクロを実行すると簡単に展開できる
氏名を展開する 例 山田一郎 → 山田,一郎(カンマを手動で挿入)→ マクロ実行→ 山田 一郎
Sub カンマ区切り文字列を展開()
'
'選択したカンマ区切り文字列を(右側)に展開する
'
Dim 文字列 As Variant
Dim 文字 As Variant
Dim i As Integer
Dim cell_row As Integer
Dim cell_column As Integer
i = 1 ' 1ならアクティブセルの右側に展開、0ならアクティブセルから展開
'アクティブセルの行列を調べる
cell_row = ActiveCell.Row
cell_column = ActiveCell.Column
'Split関数 for each で展開
文字列 = Split(ActiveCell, ",") 'ActiveCellの文字列をカンマで区切って配列にする
For Each 文字 In 文字列
Cells(cell_row, cell_column + i) = 文字 '文字(0),文字(1),文字(2)・・・・・・・・
i = i + 1
Next
End Sub
Split(文字列,区切り文字)
For Each 変数(要素) In グループ(配列) ・・・・ Next
アクティブセル行番号 ActiveCell.Row A1なら1
アクティブセル列番号 ActiveCell.Column B2なら2
アクティブセルアドレス ActiveCell.Address $A$1
Sub 選択範囲列のカンマ区切り文字列を展開()
'
'選択範囲列(1列)のカンマ区切り文字を右側に展開する
'
Dim 文字列 As Variant
Dim 文字 As Variant
Dim i As Integer
Dim j As Integer
Dim x As Integer
Dim cell_row_top As Integer
Dim cell_row_bottom As Integer
Dim cell_count As Integer
Dim cell_column As Integer
x = 0 '0で選択範囲から展開 1で右に展開
j = 0
'選択範囲を調べる
cell_column = Selection.Column
cell_row_top = Selection.ROW
cell_count = Selection.Rows.count
'Split関数 for each で展開
For i = cell_row_top To cell_row_top + cell_count - 1
文字列 = Split(Cells(i, cell_column).Value, ",")
For Each 文字 In 文字列
Cells(i, cell_column + j + x) = 文字
j = j + 1
Next
j = 0
Next i
End Sub
コメント 0