Home > database >  Insert Rows Between Specific Rows in Excel
Insert Rows Between Specific Rows in Excel

Time:01-06

By using Data from another sheet I need to insert rows.

For example, if there are data in column 10 using values from those cells as row number I need to insert columns.

Example

This is the code I have and it wont work

Sheets("Input").Select
Rows("11:11").Select
Selection.Copy

Dim x As Integer
For x = 1 To 12

Dim Hdr As Integer

Hdr = Worksheets("Input").Cells(10, x).Value

Sheets("Data Table").Select
Rows("Hdr:Hdr").Select
Selection.Insert Shift:=xlDown
Next x

CodePudding user response:

avoid Select/Selection and use explicit references to object use Rows(Hdr)

With Worksheets("Input") ' reference "Input" sheet

    .Rows("11:11").Copy ' copy referenced sheet row 11

    Dim x As Long
    For x = 1 To 12
    
        Dim Hdr As Long
        Hdr = .Cells(10, x).Value ' store the value in referenced sheet cell in row 10 and column x
        
        Sheets("Data Table").Rows(Hdr).Insert Shift:=xlDown
    Next

End With
  • Related