VBA to copy column headers and insert multiple times in rows

anglais428

Well-known Member
Joined
Nov 23, 2009
Messages
634
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am looking to convert this:

Section\ Year12345
A

<tbody>
</tbody>

by a specific value (say 2 for example) into:

SectionYr
A1
A1
A2
A2
A3
A3
A4
A4
A5
A5

<tbody>
</tbody>

I already have something working for the "sections" it is more for the years which are original across the columns and I would like them to be in 1 column for multiple rows.

Thanks,
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I wasn't too sure what you wanted but maybe we can work from the below?

Code:
Dim Repeat As Long
Dim rLastCell As Range
Dim lColumn As Long
Dim i As Long
'How many times to copy
Repeat = 3


Set rLastCell = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
lColumn = rLastCell.Column


For i = 1 To (lColumn - 1)
Range("B" & 2 + ((i - 1) * Repeat)).Resize(Repeat).Value = Cells(1, i + 1)
Next
Range("A2").Resize((i - 1) * Repeat) = "A"
 
Last edited:
Upvote 0
This is almost exactly what I need. What I actually need (and it wasn't in my original post) was to ignore a column that is between the "section/year" and "2000". How can I tinker the code to only take into account the last 5 (out of 7) column headers?

many thanks
 
Upvote 0
Ah I see.

I have added a new variable called Start that determines the column we start with.

Rich (BB code):
Dim Repeat As Long
Dim Start As Long
Dim lColumn As Long
Dim i As Long
'How many times to copy
Repeat = 6
'Which column to start with
Start = 2
'Which column to end with
lColumn = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column


For i = 1 To (lColumn - 1)
Range("B" & 2 + ((i - 1) * Repeat)).Resize(Repeat).Value = Cells(1, i + Start)
Next
Range("A2").Resize((i - Start) * Repeat) = "A"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,209
Members
448,874
Latest member
b1step2far

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