Why aren't my newly inserted columns being populated with the Formula?

Milbourn

New Member
Joined
May 27, 2014
Messages
44
Hey guys!

First the code:

Code:
Sub MyInsertColumn()
 
Dim wks As Worksheet
Dim i As Long
Dim Lastrow As Long
Application.ScreenUpdating = False


Lastrow = Range("A" & Rows.Count).End(xlUp).Row


For Each wks In ActiveWindow.SelectedSheets
    With wks
        For i = .Cells.SpecialCells(xlCellTypeLastCell).Column To 2 Step -1
            .Columns(i).Insert
            Range(Cells(2, i), Cells(Lastrow, i)).Formula = "=SUM(a2,b2)"
            .Columns(i).Insert
            Range(Cells(2, i), Cells(Lastrow, i)).Formula = "=SUM(a2,b2)"
        Next i
    End With
Next wks


End Sub

So I have this to add two columns and populate with formulas for the entire worksheet

The code runs fine, and adds the columns but does not add the formulas. Is there something I'm missing?

Thanks,

M
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
It is adding the formulae, but it's only adding them to whatever sheet is active when you run the code.
Also unless you only have 4 columns of data it's adding more than 2 columns.
Can you please explain what you are trying to do.
 
Upvote 0
Hi Fluff!

First, thanks for taking the time to reply! I appreciate it.

So I am trying to add in 2 columns next to every column in a given worksheet and then add some formulas in those new columns.

Right now this code is adding in the 2 new columns as I'd hope for on the active sheet, but no formulas (and i've checked every sheet!)

Does this help? Any advice would be welcome!!

M
 
Upvote 0
It should work but I think you need to change :
For i = .Cells.SpecialCells(xlCellTypeLastCell).Column To 3 Step -1
 
Upvote 0
How about
Code:
Sub Milbourn()
   Dim Ws As Worksheet
   Dim UsdRws As Long, i As Long

   Application.ScreenUpdating = False
   For Each Ws In ActiveWindow.SelectedSheets
      With Ws
         UsdRws = .Range("A" & Rows.Count).End(xlUp).Row
         For i = .Cells(1, Columns.Count).End(xlToLeft).Column To 2 Step -1
            .Columns(i).Resize(, 2).Insert
            .Range(.Cells(2, i), .Cells(UsdRws, i + 1)).Formula = Array("=SUM(a2,b2)", "=sum(a2,b2)")
         Next i
      End With
   Next Ws
End Sub
 
Upvote 0
Code:
For i = .Cells(1, Columns.Count).End(xlToLeft).Column To [COLOR=#ff0000]3 [/COLOR]Step -1
 
Upvote 0
Fluff that's amazing it worked perfectly. Thank you so much

Footoo, thank you for your time too. I don't think the Column To 3 was the solution though i did give it a go. It provided the same result (though missed adding the new columns to Col A)

Thank you all, this really helps :)

M
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Fluff that's amazing it worked perfectly. Thank you so much

Footoo, thank you for your time too. I don't think the Column To 3 was the solution though i did give it a go. It provided the same result (though missed adding the new columns to Col A)

Thank you all, this really helps :)

M
I don't understand how it can do what you want without my suggested amendment since column B is referenced in the formula being inserted.
 
Upvote 0
As the OP is putting the same formula in all the new columns, I suspect that they are not the correct formula, but just an example.
 
Upvote 0

Forum statistics

Threads
1,214,394
Messages
6,119,263
Members
448,881
Latest member
Faxgirl

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