insert with data

KiranKM

New Member
Joined
Jul 31, 2016
Messages
2
[FONT=&quot]I have following table[/FONT]
[FONT=&quot]header1
row1
row2
row3[/FONT]

[FONT=&quot]need to convert as below[/FONT]
[FONT=&quot]header1
row1
header1
row2
header1
row3[/FONT]

[FONT=&quot]Can anyone share code for this….note the row count is changing every time you insert[/FONT]
[FONT=&quot]Thanks.[/FONT]
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Does this do what you want?

Code:
Sub Convert_With_Headers()
Dim D As Variant
Dim S As String
Dim i As Integer, count As Integer


With ActiveSheet
    S = .Range("A1").Value  'the header location
    D = .Range("A2:A" & .Cells(.Rows.count, "A").End(xlUp).Row)
    count = 1


    For i = 1 To 2 * UBound(D) Step 2
        .Range("A" & i).Value = S
        .Range("A" & i + 1).Value = D(count, 1)
        count = count + 1
    Next
End With
End Sub
 
Upvote 0
Welcome to the MrExcel board!

In case you may have formulas, or formatting, or more than one column of data, you might also consider this alternative.

Rich (BB code):
Sub InsertHeaders()
  Dim rws As Long, nc As Long
  
  Application.ScreenUpdating = False
  nc = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, SearchFormat:=False).Column + 1
  With Range("A2", Range("A" & Rows.count).End(xlUp)).Resize(, nc)
    rws = .Rows.count
    .Columns(nc).Value = Evaluate("row(" & .Address & ")")
    .Columns(nc).Offset(rws).Resize(rws - 1).Value = .Columns(nc).Value
    With .Resize(2 * rws - 1)
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
      .Columns(1).SpecialCells(xlBlanks).Value = .Cells(0, 1).Value
      .Columns(nc).ClearContents
    End With
  End With
  Application.ScreenUpdating = True
End Sub

Before:
Excel Workbook
ABC
1header1header2header3
2row1data1
3row2data2
4row3data3something
5row4data4
6row5data5
7row6data6
8row7data7
9row8data8
10row9data9
11row10data10
12row11data11
Insert repeat headers



After:
Excel Workbook
ABC
1header1header2header3
2row1data1
3header1
4row2data2
5header1
6row3data3something
7header1
8row4data4
9header1
10row5data5
11header1
12row6data6
13header1
14row7data7
15header1
16row8data8
17header1
18row9data9
19header1
20row10data10
21header1
22row11data11
Insert repeat headers
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top