VBA code to exclude macro performing on specified worksheet

mrexcelhelp123

New Member
Joined
Mar 10, 2010
Messages
38
Can someone help me find out what is wrong with this code? I need some changes made on all worksheets (except the ones listed) for all open workbooks.

Code:
Sub Step2_Modify_Summary_Tabs()
Dim wb As Workbook
Dim ws As Worksheet
 
For Each wb In Workbooks
 
    For Each ws In Worksheets
    
    If ws <> "Sheet1" _
    Or ws <> "Sheet2" _
    Or ws <> "Sheet3" _

    Then
 
    .Range("A1:A2").ClearContents
 
    End If
    Next ws
    
Next wb
    
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try...

Code:
Sub Step2_Modify_Summary_Tabs()

    Dim wb As Workbook
    Dim ws As Worksheet
     
    For Each wb In Workbooks
        For Each ws In wb.Worksheets
           If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" And ws.Name <> "Sheet3" Then
               ws.Range("A1:A2").ClearContents
           End If
        Next ws
    Next wb
    
End Sub
 
Upvote 0
You might want to think about using the worksheets' code names, so if the sheets are ever renamed, your code will still work -- if the sheet(s) are ever deleted and recreated, this won't work, however.
 
Upvote 0
Hi Tazguy37, Domenic is offline but thought you might be able to help. If I take the his code one step further by specifying workbooks I don't want the macro to work on would I do it this way:

Code:
Sub Step2_Modify_Summary_Tabs()
 
    Dim wb As Workbook
    Dim ws As Worksheet
 
    For Each wb In Workbooks
 If wb.Name <> "Workbook1" Then
 
        For Each ws In wb.Worksheets
           If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" And ws.Name <> "Sheet3" Then
               ws.Range("A1:A2").ClearContents
           End If
        Next ws
End If
    Next wb
 
End Sub
 
Upvote 0
Domenic, it doesn't seem to be working. Here's my code:
I feel like there should be something in front of Workbooks similar to Worksheet (i.e. wb.Worksheets)

Code:
Sub Step2_Modify_Summary_Tabs()
 
    Dim wb As Workbook
    Dim ws As Worksheet
 
    For Each wb In Workbooks
 If wb.Name <> "Workbook1" Then
 
        For Each ws In wb.Worksheets
           If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" And ws.Name <> "Sheet3" Then
               ws.Range("A1:A2").ClearContents
               ws.Range("A5").Formula = "=A3+A4"
           End If
 
        Next ws
End If
    Next wb
 
End Sub
 
Upvote 0
If "Workbook1" is saved, you'll need to include its file extension. For example...

"Workbook1.xlsx"
 
Upvote 0
The below is the code I used to perform macros on all sheets in my workbook, now in this code how do I exclude one sheet that I want to use as my content page?

Sub Fixed_Assets_Recons()
Dim xsheet As Worksheet
For Each xsheet In ThisWorkbook.Worksheets
xsheet.Select
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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