Macro to Insert a formula in all worksheets

DBWright

New Member
Joined
Mar 11, 2011
Messages
2
I found this macro in the forums here to add a formula in 4 different worksheets. Is there a way to simply have the macro insert the formulas in ALL worksheets within the workbook that are not hidden instead of just the 4 listed?

PHP:
Sub Insert_SUM_Formulas()
 
    Dim ws As Worksheet
    Dim Lastrow As Long, Lastcol As Long
 
    For Each ws In Sheets(Array("Name1", "Name2", "Name3", "Name4"))
 
        Lastrow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        Lastcol = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
 
        Range(Cells(Lastrow, "D"), Cells(Lastrow, Lastcol)).FormulaR1C1 = "=SUM(R1C:R" & Lastrow - 1 & "C)"
 
    Next ws
 
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Change
Code:
For Each ws In Sheets(Array("Name1", "Name2", "Name3", "Name4"))
to
Code:
For Each ws In ThisWorkbook.Worksheets
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

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