How to Insert Columns Based on two Months

GuardianEnzo

New Member
Joined
Jun 27, 2017
Messages
11
I am trying to create a VBA function that take two inputs (months) and insert columns that fill in the range of months. IE. If I input April in one cell, and September in another, I would like the macro to insert:

April May June July August September

I've been trying out a method where I assign each month a number and then subtract the two numbers to determine how many columns to enter, but I've been coming up short.

Any help is appreciated!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
Code:
Sub InsertMonths()

    Dim Cnt As Long
    Dim Col As Long
    
    Columns(5).Resize(, Range("A2").Value - Range("A1").Value + 1).Insert
   
    For Cnt = Range("A1").Value To Range("A2").Value
     Range("E1").Offset(, Col).Value = MonthName(Cnt)
    Col = Col + 1
    Next Cnt

End Sub
This will insert columns before col E based on the values in cells A1 & A2

PS this will only work if the months are in the same year
 
Last edited:
Upvote 0
How about
Code:
Sub InsertMonths()

    Dim Cnt As Long
    Dim Col As Long
    
    Columns(5).Resize(, Range("A2").Value - Range("A1").Value + 1).Insert
   
    For Cnt = Range("A1").Value To Range("A2").Value
     Range("E1").Offset(, Col).Value = MonthName(Cnt)
    Col = Col + 1
    Next Cnt

End Sub
This will insert columns before col E based on the values in cells A1 & A2

PS this will only work if the months are in the same year

Thanks for the input!

I tried using your code, replacing your values with mine:

Code:
Sub CreateMonths()

    Dim Cnt As Long
    Dim Col As Long
    
    Columns(25).Resize(, Range("B3").Value - Range("A3").Value + 1).Insert
   
    For Cnt = Range("A3").Value To Range("B3").Value
     Range("Y15").Offset(, Col).Value = MonthName(Cnt)
    Col = Col + 1
    Next Cnt


End Sub

I get an error:

Invalid Procedure Call or Argument

on this line:

Range("Y15").Offset(, Col).Value = MonthName(Cnt)

It will then create a ridiculous amount of columns (200+) where I wanted the columns inserted.
I have a feeling it is creating a column for each individual date instead of month
 
Last edited:
Upvote 0
I suspect that you are putting dates into A3 & B3, you need to put in the number of the month.
Alternatively use
Code:
Sub CreateMonths()

    Dim Cnt As Long
    Dim Col As Long
    
    Columns(25).Resize(, Range("B3").Value - Range("A3").Value + 1).Insert
   
    For Cnt = month(Range("A3")) To month(Range("B3"))
     Range("Y15").Offset(, Col).Value = MonthName(Cnt, [COLOR=#ff0000]True[/COLOR])
    Col = Col + 1
    Next Cnt


End Sub
The part in red will give abbreviated month names if you prefer
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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