Insert Formula in every sheet in a workbook and remove formatting.

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
168
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

I have got a macro that inserts a new row in each and every sheet within a workbook using the following:-

VBA Code:
For Each ws In Worksheets
With ws
.Rows(2).Insert Shift:=xlDown
Rows("2:2").Select
End With
Next

Can someone help with inserting a formula in each and every sheet in H2 where the formula is =AVERAGE(H2:Last Row)

Where last row is the last row that has data. Please note some sheets may not have any data after row 3.

Additionally, having inserted row 2 in each sheet, I note that cells are formatted with the colour and fill colour from the previous row. Is there any way that the fill colour is set to “No Fill” and the colour is set to black?

Can what I am asking for be coded with the loop I have?

Thanks in advance for any assistance.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hello Kayslover,
tell me what you think about...

VBA Code:
Sub InsertFormulaH2()

    Dim ws As Worksheet
    Dim varNRows As Long
  
    For Each ws In Worksheets
        varNRows = ws.Cells(Rows.Count, "H").End(xlUp).Row
        With ws
            .Rows(2).Insert Shift:=xlDown
            .Range("H2").Formula = "=AVERAGE(H3:H" & varNRows + 1 & ")"
            .Rows("2:2").Interior.Color = xlNone
            .Range("H2").Font.Color = RGB(0, 0, 0)
        End With
    Next

End Sub
 
Last edited:
Upvote 0
Solution
Excel Max,

Worked a treat, I appreciate your kind assistance.
 
Upvote 0
Excel Max,

One more question comes to mind. How do I exclude sheets called Buylist and Lookup from being worked on in the loop?
 
Upvote 0
How about
VBA Code:
   For Each ws In Worksheets
      If ws.Name <> "Buylist" And ws.Name <> "Lookup" Then
         varNRows = ws.Cells(Rows.count, "H").End(xlUp).Row
         With ws
            .Rows(2).Insert Shift:=xlDown
            .Range("H2").Formula = "=AVERAGE(H3:H" & varNRows + 1 & ")"
            .Rows("2:2").Interior.Color = xlNone
            .Range("H2").Font.Color = RGB(0, 0, 0)
         End With
      End If
   Next ws
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
I'm glad that you are satisfied with solutions.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
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