2016-09-15 4 views
0

見出しにラベルと異なる長さの値のセットを含む多数の列を持つデータセットがあります。私は各値の範囲に対応するラベルを次の新しい列に挿入したいと思います。各列の後に絶対参照で空の列を挿入するVBA

私の入力データ・セット:

A  B  C 
cat dog fox 
red black yellow ....... 
red white yellow ....... 
grey black yellow ....... 
.......................................... 

私の出力は次のようになります。最初のステップで

A B  C  D  E  F ....... 
red cat black dog yellow fox ....... 
red cat white dog yellow fox ....... 
grey cat black dog yellow fox ....... 
.......................................... 

を、私は、各列の後に空白列を挿入するマクロを使用:

Public Sub Insert_Blank_Column() 

Dim wks As Worksheet 
Dim iCol As Long 

For Each wks In ActiveWindow.SelectedSheets 

    With wks 
     For iCol _ 
     = .Cells.SpecialCells(xlCellTypeLastCell).column To 1 Step -1 
     .Columns(iCol).Insert 
     Next iCol 
    End With 

Next wks 

End Sub 

しかし、新しい列ごとに「相対」絶対参照を作成する方法がわかりません。

答えて

1
Public Sub Insert_Columns() 

    Dim wks As Worksheet 
    Dim iCol As Long 

    For Each wks In ActiveWindow.SelectedSheets 

     With wks 

      For iCol = .Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1 

       'Add a column to the right of each column. 
       .Cells(1, iCol + 1).EntireColumn.Insert 

       'Fill the new column with the value from the first row of the column to its left. 
       Range(.Cells(2, iCol + 1), .Cells(.Cells(1, iCol).End(xlDown).Row, iCol + 1)) = .Cells(1, iCol) 

      Next iCol 

      'Delete the first row. 
      .Cells(1, 1).EntireRow.Delete 

     End With 

    Next wks 

End Sub 
+0

ありがとうございました!完璧に動作します。 – In777

関連する問題