SynTax Issues for Clearing a Specific Sheet

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Code:
Sub Clear_Eng_1()
Dim sht As Worksheets
For Each sht In ActiveWorkbook.Worksheets
    If (sht.Name <> "ECD") And (sht.Name <> "Revisions") Then
    sht.Activate
        With Range("A7:AV3000")
            .Clear
            .NumberFormat = "General"
            .Interior.Pattern = xlNone
            Range("A8").Interior.ColorIndex = 6
            Range("A7") = "Project Tags"
            Range("B8").Interior.ColorIndex = 4
'            Range("B7") = "MES_Data"
'            .ColumnWidth = 8.14
'            Columns("A:B").EntireColumn.AutoFit
        End With
    End If
Next sht
End Sub


Hello All,
I have a sheet in a Workbook named "REV Eng 1"
I have several other tabs.
I wish to clear only REV Eng 1 sheet, however, when I run the above code, I get a mismatch error on line: For Each sht In ActiveWorkbook.Worksheets.
What is my syntax error?

Thanks for the help
excel 2013
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Change type of sht variable to refer to a single sheet - Worksheet is a single sheet, Worksheets is a collection.
Code:
Dim sht as Worksheet[COLOR=#ff0000][B]s[/B][/COLOR]
 
Upvote 0
Code:
Dim sht as Worksheet[COLOR=#ff0000][B]s[/B][/COLOR]

Got it..Thanks
Working Correctly now
 
Upvote 0
Code:
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets

So getting back to this, Worksheet vs Worksheets continues to be confusing for me.
If Worksheet is for a single sheet, then why in the second line of code, I use Worksheets, not Work sheet, since the code will search all Worksheets, but only work one at a time?
Seems I would use: For Each sht In ActiveWorkbook.Worksheet, not ActiveWorkbook.Worksheets.

Thanks for the help
 
Upvote 0
For variable declaration, you need to write Worksheet (singular) - the variable needs to refer to only one worksheet at a time in the loop.

For the loop, you need to write ActiveWorkbook.Worksheets (plural) to refer to the collection of all worksheets in the workbook. The loop then iterates over all those sheets, assigning them to variable sht one sheet in each iteration.
 
Upvote 0
Ahhh...didn't look at it thatway.
Thanks for the help
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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