Macro to create dynamic headers

ANE0709

Board Regular
Joined
Feb 2, 2022
Messages
65
Office Version
  1. 365
Platform
  1. Windows
since discovering this site you guys have been super helpful so i came back for more.

im building a worksheet with VBA and have the foundation all laid out. my current code takes data that is exported from an external source, cleans it up, adds formulas, and does a whole lot of calculations. My problem is specifically the headers... Currently i created the headers from scratch and do not have a working vba to help create them going forward. I need some sort of code that will dynamically generate the headers for me if possible. Any help is appreciated.

- Only Columns M1-AN1 need to be generated using VBA
- this is a 25 month report
- I need to concatenate the year and the month. the year is constant but the month shifts left as the oldest month falls off column M and the new month is added to column AM

Raw Data for January
1646166016224.png


Raw Data for February
1646166035929.png


What the final should look like. Always M1-AN1
1646166055498.png
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
VBA Code:
Sub Months_25()
     Range("A1").Name = "StartDate"                             'named range, this cell contains a date in the 1st month
     arr = [transpose(substitute(text(Eomonth(startdate,row(a1:a25)-1),"'mmmm-yyyy"),"-",char(10)))]     '25 months, replace the "-" by a vblf
     Range("M1").Resize(, UBound(arr)).Value = arr
End Sub
 
Upvote 0
VBA Code:
Sub Months_25()
     Range("A1").Name = "StartDate"                             'named range, this cell contains a date in the 1st month
     arr = [transpose(substitute(text(Eomonth(startdate,row(a1:a25)-1),"'mmmm-yyyy"),"-",char(10)))]     '25 months, replace the "-" by a vblf
     Range("M1").Resize(, UBound(arr)).Value = arr
End Sub
thank you. I tweaked it a tiny bit and it worked.
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,923
Members
449,094
Latest member
teemeren

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