Issue with VBA running on multiple worksheets

Worker8ee

New Member
Joined
Aug 8, 2018
Messages
28
Hey everyone, I am trying to run the following VBA for all sheets in my workbook but it is only processing the tab that is selected at the time the VBA starts. I suspect it is because I don't have it formatted properly since I recorded a macro to inform this VBA but I don't have enough experience to understand how it is formatted improperly. Any suggestions/help would be greatly appreciated!

VBA Code:
Sub PriorQuarterTotals ()     
Dim ws As Worksheet
     For Each ws In ActiveWorkbook.Worksheets
    Columns("Q:Q").Select
    Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Range("A1").Select
    Next ws
 
    
 For Each ws In ActiveWorkbook.Worksheets
    Range("E2").Select
    ActiveCell.FormulaR1C1 = _
        "=""***Prior Quarter Totals: $""&TEXT(ROUND(SUMIFS('[Inventory VBA.xlsm]2020 Q2 All'!R4C8:R60000C8,'[Inventory VBA.xlsm]2020 Q2 All'!R4C6:R60000C6,1,'[Inventory VBA.xlsm]2020 Q2 All'!R4C14:R60000C14,R4C14),2),""#,##0_ ;-#,##0 "")&""***"""
    Range("E2").Select
    Selection.Font.Bold = True
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("E2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = _
        "=""***Prior Quarter Retail Totals: $""&TEXT(ROUND(SUMIFS('[Inventory VBA.xlsm]2020 Q2 All'!R4C8:R60000C8,'[Inventory VBA.xlsm]2020 Q2 All'!R4C6:R60000C6,1,'[Inventory VBA.xlsm]2020 Q2 All'!R4C14:R60000C14,R4C14),2),""#,##0_ ;-#,##0 "")&""***"""
    Range("E2").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
        Next ws
        
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about
VBA Code:
Sub PriorQuarterTotals()
   Dim ws As Worksheet
   For Each ws In ActiveWorkbook.Worksheets
      ws.Columns("Q:Q").Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
         ReplaceFormat:=False
   Next ws
   
   
   For Each ws In ActiveWorkbook.Worksheets
      With ws.Range("E2")
         .FormulaR1C1 = _
            "=""***Prior Quarter Retail Totals: $""&TEXT(ROUND(SUMIFS('[Inventory VBA.xlsm]2020 Q2 All'!R4C8:R60000C8,'[Inventory VBA.xlsm]2020 Q2 All'!R4C6:R60000C6,1,'[Inventory VBA.xlsm]2020 Q2 All'!R4C14:R60000C14,R4C14),2),""#,##0_ ;-#,##0 "")&""***"""
         .Value = .Value
         .Font.Bold = True
      End With
   Next ws
End Sub
 
Upvote 0
Yes that did the trick! Thank you Fluff!!! I will make sure to study this format, you have saved me!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,042
Members
448,940
Latest member
mdusw

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