Excel2000 VBA 文字列をInputBoxに入力した文字で展開 [Excel2000 VBA独習]
Excel2000 VBA 文字列をInputBoxに入力した文字で展開
急ぎで書いたので、検証不十分(いつものことですが・・・)
Sub 文字列展開()
'文字列をINPUTBOXに入力キーワードで展開する
'例 東京都新宿区 → キーワード(都) 東京都 新宿区
'始めに文字列を展開する範囲1列を選択しておく
'
'エラーバグはその都度修正
Dim addr, leftStr, rightStr As String
Dim strPos, strSize As Long
Dim str1, str2 As Variant
Dim x As Variant
Dim j, i As Long
Dim cell_row_top As Long
Dim cell_count As Long
Dim cell_column As Long
Dim myIB As String
Dim keyWord As Variant
'選択範囲を調べる
cell_column = Selection.Column
cell_row_top = Selection.Row
cell_count = Selection.Rows.count
'inputbox区切り文字キーワード取得
myIB = Application.InputBox(Title:="区切り文字キーワードで展開", prompt:="区切り文字キーワードを入力して下さい。", Type:=2)
keyWord = Array(myIB)
'
'選択列 1行目から終わりまで処理を繰り返す
For i = cell_row_top To cell_row_top + cell_count - 1
addr = Cells(i, cell_column).Value
For Each x In keyWord
Debug.Print x
strSize = Len(addr)
strPos = InStr(addr, x)
'区切り文字の次にカンマを追加した文字列作成
If strPos = 0 Then GoTo n
leftStr = Left(addr, strPos)
rightStr = Right(addr, strSize - strPos)
addr = leftStr + "," + rightStr
n:
Next x
'カンマ区切り文字展開
str1 = Split(addr, ",")
x = 1 'x=0 選択セルから展開 x=1 選択セルの右側から展開
For Each str2 In str1
Cells(i, cell_column + x).Value = str2
x = x + 1
Next str2
Next i
End Sub
コメント 0