Loop though workbook and insert a function in each worksheet

Bamerand

Board Regular
Joined
Jan 11, 2013
Messages
62
Hi there,

Can anybody assist me with the following? I need to re-develop a macro that enters a formula into each worksheet within a workbook. The workbook has 25 worksheet and I need to insert a formulat that divided the last reading in the columns "C", "G", "E" and "I". Here is what I was able to come with:

Sub Test()
Dim rng As Range

Set rngC = Range("C" & Cells(Rows.Count, "C").End(xlUp).Row)
Set rngE = Range("E" & Cells(Rows.Count, "E").End(xlUp).Row)
Set rngG = Range("G" & Cells(Rows.Count, "G").End(xlUp).Row)
Set rngI = Range("I" & Cells(Rows.Count, "I").End(xlUp).Row)

rngC.Offset(1, 0).Value = rngC / 14.5
rngE.Offset(1, 0).Value = rngE / 14.5
rngG.Offset(1, 0).Value = rngG / 14.5
rngI.Offset(1, 0).Value = rngI / 14.5

End Sub

When I run the code, it work perfectely and inserts the formula into desired destination (divides last value in any of the columns "C", "G", "E" and "I"). But, what I can not do is make code loop so that the macro is applied for the entire workbook and spares me from running the-present code at each worksheet individually. I tried this code:

Sub LoopTest()
Dim Current As Worksheet
For Each Current In Worksheets
' here i pasted the macro "Test"
Next
End Sub

But it did not work. Can anybody give a hint on how should I make it run as it should?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Thanks for the Feedback

Note that if you again want to run the code then first delete the sheet name of Result bcoz macro will try to add it again it will show error
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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