2016-07-23 2 views
0

10 +列の1Kレコードを含むExcel 2007シートがあります。問題は、1つのセルに複数のデータが含まれているため、各セルに移動する必要があります。データをシフトすると、残りの行と重なりが中断されるため、新しい行を挿入する必要があります。彼らはそれを行うVBAコードまたはマクロプロセスですか?助けてください。複数のデータセルを下に移動して下に行を挿入する

ここは優れたシートです。

Category | Desciption  | Sizes  | Price 
------ | ------ 
car  | Car Description | 123 - M | $20 
           1245 - XL | $50 
           1243 - XXL| $55 
Car2  | Car2 Description | 123 - M | $20 
           1245 - XL | $50 
           1243 - XXL| $55 

私が達成したいと思っていることを希望します。 SIzes列データは1つのセルにあり、以下の残りのデータを乱さないように行を挿入しながらこれらをシフトする必要があります。

多くのありがとうございます。あなたは(コメントを参照)、このコード試すと適合させることができ ハルーン

答えて

1

:私はそれを実行したときに

Option Explicit 

Sub main() 
    Dim iRow As Long, nRows As Long, nData As Long 
    Dim arr As Variant 

    With Worksheets("data").Columns("C") '<--| assuming "Sizes" are in column "C" 
     nRows = .Cells(.Rows.Count, 1).End(xlUp).row '<--| get column "C" last non empty row 
     For iRow = nRows To 2 Step -1 '<--| loop through column "C" rows from the last one upwards 
      With .Cells(iRow) '<--| reference column "C" current cell 
       arr = Split(.Value, vbLf) '<--| try and split cell content into an array with "linefeed" character as delimeter 
       nData = UBound(arr) + 1 '<--| count array items (Split generates 0-based array, so a 1-element array upperbound is 0) 
       If nData > 1 Then '<--| if there are more than 1 ... 
        .EntireRow.Offset(1).Resize(nData - 1).Insert '<--| insert rows beneath current cell 
        .Resize(nData).Value = Application.Transpose(arr) '<--| fill current cell and new ones beneath it with array values (you have to transpose it, since array is a 1-row array while you're writing into a 1-column range) 
        .Offset(, 1).Resize(nData).Value = Application.Transpose(Split(.Offset(, 1).Value, vbLf)) '<--| do the same filling with adjacent cell 
       End If 
      End With 
     Next iRow 
    End With 
End Sub 
+0

私は型の不一致エラーが発生します。それは動作していますが、一度ループを完了すると、RUN TIME ERROR 13:タイプの不一致で終わります。私は続けていくために 'F8'を繰り返していく必要があります。そのための修正点 – HWQ

+0

関数内で "隣接セル"を見つけました。ありがとう。今働いている。 – HWQ

+0

あなたは歓迎です – user3598756

関連する問題