Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

insert columns with formatting and formulas

0

Hello

 I  search  for  solve  this  problem  automaically instead  of  using manually.

based  on  the  macro will insert three columns for next month with the same formatting and formulas . the  problemis not adding formulas for row TTL for columns Arrived & Sales  just  run  the  macro  you  will  see  the  formuals  is  gone  for row TTL for columns Arrived & Sales  .

Sub InsertMonth()
    Dim rng As Range
    Dim oldmth As String, mystr As String
    Dim oldm As Integer, newm As Integer
    Dim am

    am = [{"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"}]
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    With Sheets(1)
        oldmth = (.Cells(1, Columns.Count).End(xlToLeft)(1, 1))
        oldm = Application.Match(oldmth, am, 0)
        newm = Month(DateSerial(Year(Now), oldm + 1, 1))

        .Cells(2, Columns.Count).End(xlToLeft)(1, -1).Resize(.[A2].CurrentRegion.Rows.Count - 1, 3).Copy _
                Cells(2, Columns.Count).End(xlToLeft)(1, 2)
        .Cells(2, Columns.Count).End(xlToLeft)(1, -1).Offset(1).Resize(.[A2].CurrentRegion.Rows.Count, 1).Resize(, 3).ClearContents
        If .Cells(1, Columns.Count).End(xlToLeft)(1, 1).Value <> "JANUARY" Then
            .Cells(2, Columns.Count).End(xlToLeft)(1, -1).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).Copy
            .Cells(2, Columns.Count).End(xlToLeft)(1, 1).Offset(1).Resize(.[A2].CurrentRegion.Rows.Count, 1).PasteSpecial xlPasteFormulas

        End If
        .Cells(1, Columns.Count).End(xlToLeft)(1, 1).Offset(, 1).Value = _
                        Choose(newm, "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER")
        With .Cells(1, Columns.Count).End(xlToLeft)(1, 1).Resize(, 3)
            .Merge
            .HorizontalAlignment = xlCenter
            .Font.ColorIndex = 1
            .Interior.ColorIndex = 6
             .Font.Size = 12

        End With
        .Cells(2, Columns.Count).End(xlToLeft)(1, 1).Offset(1).Select
        Application.CutCopyMode = False
        Application.Calculation = xlCalculationAutomatic
    End With
End Sub
Answer
Discuss

Answers

0
Selected Answer

Hello Leopard,

>> updated Dec. 9/2022 (see below)

It took me a few tries but I figured it out.

You only need to make a couple of small changes. Your code only copies the "Stock" column; that is why the are no formulas in the "Arrived" and "Sales" columns. Here are the changes needed:

' changed these two lines
'        .Cells(2, Columns.Count).End(xlToLeft)(1, -3).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).Copy
'            .Cells(2, Columns.Count).End(xlToLeft)(1, 1).Offset(1).Resize(.[A2].CurrentRegion.Rows.Count, 1).PasteSpecial xlPasteFormulas

' to the following  (copy formulas one column at a time)

             ' "Arrived" column formulas
            .Cells(2, Columns.Count).End(xlToLeft)(1, -3).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).Copy
            .Cells(2, Columns.Count).End(xlToLeft)(1, 0).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).PasteSpecial Paste:=xlPasteFormulas
            .Cells(2, Columns.Count).End(xlToLeft)(1, 0).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).SpecialCells(xlCellTypeConstants, 23).ClearContents

             ' "Sales" column formulas
             .Cells(2, Columns.Count).End(xlToLeft)(1, -2).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).Copy
             .Cells(2, Columns.Count).End(xlToLeft)(1, 1).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).PasteSpecial xlPasteFormulas
             .Cells(2, Columns.Count).End(xlToLeft)(1, 1).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).SpecialCells(xlCellTypeConstants, 23).ClearContents

            ' "Stock" column formulas
            .Cells(2, Columns.Count).End(xlToLeft)(1, -1).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).Copy
            .Cells(2, Columns.Count).End(xlToLeft)(1, 2).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).PasteSpecial xlPasteFormulas

The xlPasteFormulas treats numbers as formulas. That is why after doing the pasting for "Arrived" and "Sales" there is the extra line of code to remove constants.

The modified file is attached. (I also corrected the circular reference in V37)

If this solves your problem then please mark my answer as selected.

Update: re-wrote macro (due to a sugestion from John)

New macro is "NewMonth_2" and is in Module2 (updated file attached)

Here is the new macro code:

Sub NewMonth_2()

' macro written by Doug Wilson for TeachExcel forum question, Dec. 2022
' this macro was written due to a suggestion by John_Ru
' this macro is a simpler version of Leopard's original

    Dim rng As Range
    Dim oldmth As String, mystr As String
    Dim oldm As Integer, newm As Integer
    Dim am

    am = [{"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"}]
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    With Sheets(1)
        oldmth = (.Cells(1, Columns.Count).End(xlToLeft)(1, 1))
        oldm = Application.Match(oldmth, am, 0)
        newm = Month(DateSerial(Year(Now), oldm + 1, 1))

If .Cells(1, Columns.Count).End(xlToLeft)(1, 1).Value <> "JANUARY" Then

Dim LCol As Variant     ' last used column of sheet

LCol = Cells(2, Columns.Count).End(xlToLeft).Column

Columns(LCol).Select    ' select last month's 3 columns
    Selection.Copy
    .Columns(LCol + 1).Select   ' column to the right of last column
    Selection.Insert    ' inserts copied columns to the right
    Application.CutCopyMode = False     ' remove ~marching ants~ from around selection

' delete constants (numbers only, leave formulas) from "Arrived" and "Sales" columns
On Error Resume Next   ' this is needed in case there are no values (constants) in the column
.Cells(2, Columns.Count).End(xlToLeft)(1, 0).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 2).SpecialCells(xlCellTypeConstants, 23).ClearContents

End If

' insert new month at top of new columns
.Cells(1, Columns.Count).End(xlToLeft)(1, -2).Offset(, 1).Value = _
                        Choose(newm, "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER")

        With .Cells(1, Columns.Count).End(xlToLeft)(1, -2).Resize(, 3)
            .Merge
            .HorizontalAlignment = xlCenter
            .Font.ColorIndex = 1
            .Interior.ColorIndex = 6
             .Font.Size = 12
        End With

Cells(1, Columns.Count).End(xlToLeft).Select   ' cell at the top with the month name

' Application.CutCopyMode = False
Application.Calculation = xlCalculationAutomatic

End With


End Sub

Cheers   :-)

Discuss

Discussion

Hi Willie ,
in  reality I'm  surprised from your  way  despite  of  using  your  way  but  doesn't  work  for  me    I    no  know   what  my  mistake  then.
thanks  very  much  for  your  help
leopard (rep: 88) Dec 9, '22 at 9:29 am
@Willie

I think your solution should be better than mine but I didn't test it. Hope you can fix the (hopefully temporary) problem for Leopard.

Incidentally, when you Paste:=xlPasteFormulas, you do it three times to column sections-  can't you resize the range to include the three columns, paste formulas then resize it to 2 columns wide then clear the Constants (just once)?
John_Ru (rep: 6142) Dec 9, '22 at 12:25 pm
When I tested the file again I discovered that if there are no values in columns "Arrived" or "Sales" the macro would hang. Added error handling to both original and new macro. No errors now.

Cheers   ;-)
WillieD24 (rep: 547) Dec 9, '22 at 5:36 pm
@Willie 
it  doesn't  show  me  error  because of   I  have  two  macros . first  I  will pull  the  values  from  another  file , second  macros  will  fill empty  cells  by  zero .  even if  that  thanks  for  this  spotting  point .
have  a good  weekend!
leopard (rep: 88) Dec 10, '22 at 3:52 am
@Leopard

I'm glad that Willie solved your problem with a neat solution.

Please note a formula error I spotted- all the Arrived and Stock totals in row 212 are incorrect, e.g. the formula in cell EP212 is:  
=SUM(EP100:EP208)

but should read:
=SUM(EP100:EP211)

and likewise for column ER (or you miss values in rows 209 and 210).

Furthermore, the Sales total is wrong, e.g. cell EQ212 reads:
=SUM(EQ3:EQ211)

but should read
=SUM(EQ100:EQ211)


I leave you to make those corrections (and the circular references, if you have the stamina!)
John_Ru (rep: 6142) Dec 10, '22 at 5:00 am
@ Willie
Good work! Thank for following my suggestion but it wasn't qiute what I meant, in that your lines:
 
' delete constants (numbers only) from "Arrived" column
On Error Resume Next   ' this is needed in case there are no values (constants) in the column
.Cells(2, Columns.Count).End(xlToLeft)(1, 0).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).SpecialCells(xlCellTypeConstants, 23).ClearContents
' delete constants (numbers only) from "Sales" column
On Error Resume Next   ' this is needed in case there are no values (constants) in the column
'.Cells(2, Columns.Count).End(xlToLeft)(1, 1).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 1).SpecialCells(xlCellTypeConstants, 23).ClearContents

  clear the columns one by one but can be replaced by (see change in bold in the Resize element) in the next discussion point (owing to length):
John_Ru (rep: 6142) Dec 10, '22 at 5:02 am
' delete constants (numbers only) from "Arrived" and "Sales" columns
On Error Resume Next   ' this is needed in case there are no values (constants) in the column
.Cells(2, Columns.Count).End(xlToLeft)(1, 0).Offset(1, -1).Resize(.[A2].CurrentRegion.Rows.Count, 2).SpecialCells(xlCellTypeConstants, 23).ClearContents

I guess you know you don't have to repeat the "On Error" statements (unless you want to change what happens, mid-module) since they apply from the end of their line.

Finally, it would help other users  it your Answer showed the final version of the code (so they don't have to look into the file).
John_Ru (rep: 6142) Dec 10, '22 at 5:03 am
@John_Ru

Thanks for your insight.
I edited the "Resize" as you suggested so both columns are handled together.
I have replaced the attached file with the one with the revised code.
I have also included/posted the code for the new macro in the original post.
Cheers   ;-)
WillieD24 (rep: 547) Dec 11, '22 at 2:18 pm
@Willie - thanks for doing that. 
John_Ru (rep: 6142) Dec 11, '22 at 3:07 pm
Add to Discussion
0

Leopard

One way is to add a new variable and a simple loop at the end of your code to write the formula on any "TTL" row (changes in bold below):

Sub InsertMonth()
    Dim rng As Range
    Dim oldmth As String, mystr As String
    Dim oldm As Integer, newm As Integer
    Dim am
    Dim Rw As Long

<< Existing code>>

        End With

        ' loop down rows
        For Rw = 3 To .[A2].CurrentRegion.Rows.Count
            If .Cells(Rw, 2).Value = "TTL" Then
                ' if column 2=TTL then copy formula from last column to 2 before
                With .Cells(Rw, .[A2].CurrentRegion.Columns.Count)
                    .Copy
                    .Offset(0, -2).Resize(, 2).PasteSpecial xlPasteFormulas
                End With
            End If
        Next Rw

        .Cells(2, Columns.Count).End(xlToLeft)(1, 1).Offset(1).Select
        Application.CutCopyMode = False
        Application.Calculation = xlCalculationAutomatic
    End With
End Sub

Hope this fixes things for you.

Discuss

Discussion

great ! thanks John for your solution and sorry !! I will select willie's answer because no loop .
leopard (rep: 88) Dec 9, '22 at 9:24 am
No problem Leopard. I think Willie's solution should be better than my rushed one but I didn't test it  and I now see you say it doesn't work for you (even though you marked it as Selected)- it might have been better to wait until the problem was found IMHO!
John_Ru (rep: 6142) Dec 9, '22 at 12:15 pm
 I think Willie's solution should be better than my rushed one but I didn't test it 
also  your  solution is very  well    too  . the  difference  is  only  you  use  loop .
and I now see you say it doesn't work for you (even though you marked it as Selected)- it might have been better to wait until the problem was found IMHO!
sorry john  about  my  bad  expression !
willie's  updating for  the  code works  great !  
what  I  meant  it  I  tried  to  modify  the  code  like  willie  did  it  with  the  same  way  but  I  failed ,that's  it .
have  a nice  weekend !
leopard (rep: 88) Dec 10, '22 at 3:44 am
Understood but was I confused by your first comment on Willie's code. Enjoy your weekend too. 
John_Ru (rep: 6142) Dec 10, '22 at 4:17 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login