Macro o insert formula in new columns

Ulisses_Carso

New Member
Joined
Sep 4, 2020
Messages
39
Office Version
  1. 365
Platform
  1. Windows
I have a sheet with X column numbers, I need to insert a column between each one and then I need to insert a formula in each new column.

I already made the code to insert these columns, but I'm stuck in the part of inserting the formula.
Note: The formula will reference and fill down with the cell to its left.

I think a loop could do this, but i don't know how to use it, can anyone help me?

VBA Code:
Sub InsertBlankColumns()
    Dim MyRange As Range
    Dim iCounter As Long
    Set MyRange = Range("A:AI")

    For iCounter = MyRange.Columns.Count To 2 Step -1
        MyRange.Columns(iCounter).EntireColumn.Insert
    LastRowColumnA = Cells(Rows.Count, 1).End(xlUp).Row
    Next iCounter
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You didn't say what formula you wanted in the column so I have put a simple "sum"
VBA Code:
Sub InsertBlankColumns()
    Dim MyRange As Range
    Dim iCounter As Long
    Set MyRange = Range("A:AI")
    lastrowcolumnA = Cells(Rows.Count, 1).End(xlUp).Row

    For iCounter = MyRange.Columns.Count To 2 Step -1
        MyRange.Columns(iCounter).EntireColumn.Insert
        Range(Cells(1, iCounter - 1), Cells(lastrowcolumnA, iCounter - 1)).Formula = "=sum(A1:A" & lastrowcolumnA & ")"
    Next iCounter
End Sub
 
Upvote 0
Hello Offhelip,
I just saw your message today, that line is working, i only removed that " - 1" in both iCounter and it worked with reservations.
1. I don't know why your line isn't accepting my formula i'm using an "IF" as example "if(a1<>"";"1";"2") because the real is a big IF with 15 conditions o_O
2. is there a way to insert a column after column A, the code inserts a column to its left and the formula there, but don't do it in column A

I'm trying to solve this here, but your help is very welcome again.


Here is the line i modified, i tryed to use .FormulaLocal (i think because i'm from Brasil and all my formulas are in PT-BR) instead only .Formula but din't work too

VBA Code:
Range(Cells(1, iCounter), Cells(LastRowColumnA, iCounter)).Formula = "=SE(A1<>"""";""1"";""2"")" & LastRowColumnA & ")"
I think it's missing ( but I didn't find where
 
Upvote 0
Try
VBA Code:
Range(Cells(1, iCounter), Cells(LastRowColumnA, iCounter)).Formula = "=IF(A1<>"""",""1"",""2"")"
 
Upvote 0
You did it, why i don't need that end of code "& LastRowColum....." with my formula? I didn't get it.
 
Upvote 0
You don't need it as you are just looking at a single cell in col A on each row.
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,605
Members
449,038
Latest member
Arbind kumar

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