How do I use this to repeat Column n times based on number in table?

akconant

New Member
Joined
Oct 25, 2019
Messages
1
I hope this is possible, but I want to be able to repeat column headers n times depending on the table below. I would just put the formula on the far right on the same row, but hoping it would come out like the bottom picture. Thank you if you know how to do this!

859a9f2f8286357c93923a09a9f45398-full.jpg


26qdhoG
Example of output:

2c24927de6b3ec126f12e08cff61f563-full.jpg
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi @★ akconant, welcome to the forum!

If you want, you can use the following macro.

Code:
Sub [COLOR=#0000ff]Repeat_Head[/COLOR]()
  Dim lc As Long, i As Long, j As Long, k As Long
  lc = Cells(1, Columns.Count).End(xlToLeft).Column
  Range(Cells(2, lc + 1), Cells(Rows.Count, Columns.Count)).ClearContents
  For i = 2 To Range("B" & Rows.Count).End(xlUp).Row
    For j = 2 To lc
      For k = 1 To Cells(i, j)
        Cells(i, Columns.Count).End(xlToLeft)(1, 2).Value = Cells(1, j)
      Next
    Next
  Next
End Sub

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (Repeat_Head) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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