VBA applies to selected worksheets

Apple08

Active Member
Joined
Nov 1, 2014
Messages
450
Hi All

I have a macro which is applied to all worksheets except 'configuration' or 'project managers' worksheets. However, when I run it, it seems the macro also affect these two worksheets. Please could someone help me to resolve this? Many thanks.

The macro starts with:

Code:
Sub report()
Dim sh As Worksheet
Application.ScreenUpdating = False
For Each sh In Worksheets
If sh.Name <> "configuration" Or sh.Name <> "project managers" Then
    sh.Activate
 
Oh, and one more thought... I'd stick with the FOR EACH WORKSHEET construct you started with, i.e. I would not opt for the approach Teeks2k used (for i = 1 to .count) unless I had a specific reason to expand my testing. Teeks2k used the Sheets collection, not the Worksheets collection which would open expand the scope of your loop. To me the approach you're already using is more robust.

Code:
Sub ForEachComparison()

Rem     create a workbook with a couple of worksheets
Rem     and a couple of chart sheets


    Dim objSheet    As Object, _
        wksItem     As Excel.Worksheet
    
    For Each objSheet In Sheets: Debug.Print objSheet.Name, TypeName(objSheet): Next objSheet
    Debug.Print String(20, Chr(151))
    For Each wksItem In Worksheets: Debug.Print wksItem.Name, wksItem.CodeName: Next wksItem


End Sub
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Even easier
Code:
If Ucase(sh.Name) <> "CONFIGURATION" Or sh.Name <> "PROJECT MANAGERS" Then

Yes, I was going by a code I currently use so I stuck with that format, but this is much easier for his/her purpose.
 
Last edited:
Upvote 0
Oh, and one more thought... I'd stick with the FOR EACH WORKSHEET construct you started with, i.e. I would not opt for the approach Teeks2k used (for i = 1 to .count) unless I had a specific reason to expand my testing. Teeks2k used the Sheets collection, not the Worksheets collection which would open expand the scope of your loop. To me the approach you're already using is more robust.

Could you explain the difference between sheets and worksheets collection? I thought both approaches were essentially the same thing, I am just used to using For v For Each
 
Upvote 0
Could you explain the difference between sheets and worksheets collection? I thought both approaches were essentially the same thing, I am just used to using For v For Each

Teeks,

The Sheets collection includes all of the sheets in the workbook. In modern Excel, that means worksheets and chart sheets. In ancient Excel, that also meant dialog sheets and macro sheets. One can still create dialog sheets and macro sheets, though I can't think of a good reason to do so. Run the following and note the output to the debug window. Note that a macro sheet will return a typename of "Worksheet" but a type enumeration (3 or 4) different than a normal worksheet (-4167).

Code:
Sub SheetsVersusWorksheets()

    Dim objSheet    As Object, _
        wksItem     As Excel.Worksheet, _
        s$
        
    Workbooks.Add XlWBATemplate.xlWBATWorksheet
        
    With Sheets
        .Add Type:=XlSheetType.xlWorksheet
        .Add Type:=XlSheetType.xlExcel4MacroSheet
        .Add Type:=XlSheetType.xlChart
        .Add Type:=XlSheetType.xlDialogSheet
        .Add Type:=XlSheetType.xlExcel4IntlMacroSheet
    End With
    
    On Error Resume Next
    For Each objSheet In Sheets
        Let s = objSheet.Name
        Let s = s & vbTab & TypeName(objSheet)
        Let s = s & vbTab & objSheet.Type
        If Err.Number <> 0 Then
            Let s = s & vbTab & "(no type property)"
            Err.Clear
        End If
        Debug.Print s
    Next objSheet
    
    Debug.Print String(20, Chr(151))
    
    For Each wksItem In Worksheets: Debug.Print wksItem.Name, wksItem.CodeName: Next wksItem


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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