Need my code to loop through all sheets

dwerden

New Member
Joined
Jul 23, 2005
Messages
47
Hi Masters of Excel,
I can't seem to find a way to have my code loop through all sheets in a workbook. I've never worked with loops before. What I have tried only goes to sheet 2 so I have removed that. Any thoughts to get me in the right direction. Here is the code I am hoping to execute on each sheet (up to 80 sheets in the workbook).

Code:
Sub ReviseTime()
'
' ReviseTime Macro
'
' Keyboard Shortcut: Ctrl+r

'
'Removes rows with specific text
   Dim i As Long
   Dim Ary As Variant
   Ary = Array("Other", "Total")
   For i = 0 To UBound(Ary)
      With Range("C5", Range("C" & Rows.Count).End(xlUp))
         .Replace "*" & Ary(i) & "*", True, xlPart, , False, , False, False
         On Error Resume Next
         .SpecialCells(xlConstants, xlLogical).EntireRow.Delete
         On Error GoTo 0
      End With
      With Range("D5", Range("D" & Rows.Count).End(xlUp))
         .Replace "*" & Ary(i) & "*", True, xlPart, , False, , False, False
         On Error Resume Next
         .SpecialCells(xlConstants, xlLogical).EntireRow.Delete
         On Error GoTo 0
      End With
   Next i
   
'Replace numbers greater than 4
    Columns("P:P").Select
    Selection.Replace What:="1", Replacement:="1", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="2", Replacement:="2", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="3", Replacement:="3", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="4", Replacement:="4", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="5", Replacement:="4", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="6", Replacement:="5", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="7", Replacement:="6", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="9", Replacement:="8", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Range("R7").Select
    ActiveCell.FormulaR1C1 = "=SUM(C[-2])"
    Range("R8").Select


End Sub
 
Last edited by a moderator:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Resolved this using code from Richynero that I found on the forum.

Put this before my code
Dim wsh As Worksheet


For Each wsh In Application.Worksheets

wsh.Activate

And ended it with Next wsh before End Sub.

Thank you to all who looked at this post.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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