Excel Macro Looping Issue

mralbatross

New Member
Joined
Apr 25, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,
Would anyone be able to tell me why this code doesn’t loop to the next worksheet and only performs the task on the active sheet?

I realize my code is poor but this is just a tester for a few additional tasks to be performed within the loop – but at this stage I can’t even get the loop to work!
As always thanks for all the support!


Sub Formatting()
Dim ws As Worksheet
If ws.Visible Then ws.Select (False)
Next

Cells.Select
Range("I30").Activate
With Selection.Font
.Name = "Verdana"
.Size = 10
.Strikethrough = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
With Selection.Font
.Name = "Verdana"
.Size = 10
.Strikethrough = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
ActiveWindow.Zoom = 83
Range("K30").Select

With Application.FindFormat.Interior
.PatternColorIndex = xlAutomatic
.Color = 8476672
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Application.ReplaceFormat.Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True, _
FormulaVersion:=xlReplaceFormula2

End Sub

Thanks!
 

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.
Try the changes below if you are trying to loop through the visible sheets (untested and I haven't looked at the formatting part bar removing the duplicate part)
Rich (BB code):
Sub Formatting()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Visible Then
            ws.Activate
            Cells.Select
            Range("I30").Activate
            
            With Selection.Font
                .Name = "Verdana"
                .Size = 10
                .Strikethrough = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .TintAndShade = 0
                .ThemeFont = xlThemeFontNone
            End With

            ActiveWindow.Zoom = 83
            
            Range("K30").Select

            With Application.FindFormat.Interior
                .PatternColorIndex = xlAutomatic
                .Color = 8476672
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            
            With Application.ReplaceFormat.Interior
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorLight2
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
                          xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True, _
                          FormulaVersion:=xlReplaceFormula2
        End If
    Next
End Sub
 
Upvote 0
Try the changes below if you are trying to loop through the visible sheets (untested and I haven't looked at the formatting part bar removing the duplicate part)
Rich (BB code):
Sub Formatting()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Visible Then
            ws.Activate
            Cells.Select
            Range("I30").Activate
           
            With Selection.Font
                .Name = "Verdana"
                .Size = 10
                .Strikethrough = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .TintAndShade = 0
                .ThemeFont = xlThemeFontNone
            End With

            ActiveWindow.Zoom = 83
           
            Range("K30").Select

            With Application.FindFormat.Interior
                .PatternColorIndex = xlAutomatic
                .Color = 8476672
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
           
            With Application.ReplaceFormat.Interior
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorLight2
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
                          xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True, _
                          FormulaVersion:=xlReplaceFormula2
        End If
    Next
End Sub
This worked! thanks! Is there a way to run the macro on highlighted sheets only. For example, if I select 10 of the 20 tabs, only those would be updated.
 
Upvote 0
Try...
VBA Code:
Sub Formatting()
    Dim ws As Worksheet
   For Each ws In ActiveWindow.SelectedSheets
       
            ws.Activate
            Cells.Select
            Range("I30").Activate
            
            With Selection.Font
                .Name = "Verdana"
                .Size = 10
                .Strikethrough = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .TintAndShade = 0
                .ThemeFont = xlThemeFontNone
            End With

            ActiveWindow.Zoom = 83
            
            Range("K30").Select

            With Application.FindFormat.Interior
                .PatternColorIndex = xlAutomatic
                .Color = 8476672
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            
            With Application.ReplaceFormat.Interior
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorLight2
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
                          xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True, _
                          FormulaVersion:=xlReplaceFormula2
        
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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