VBA select case

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello All,
I have this code for clearing data from certain range of some sheets in my workbook. What I want the case statement to do is to pick sheets that I want to exclude from the clearing.
Code:
Sub ClearRange()
    Dim Wsheet As Worksheet
    For Each Wsheet In Worksheets
    Select Case Wsheet.CodeName
    Case "Sheet2", "Sheet4", "Sheet6"
    Case Else
        Range(Cells(2, 1), Cells(200, 10)).Select
        Selection.ClearContents
    End Select
    Next
End Sub
The sheets listed above are those that the
code is rather clearing data from. I wish I could list those that are to be excluded instead because they are few. I need the bigger brains to help fix this challenge for me.
Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Re: VBA select case challenge

This will exclude Sheet2, Sheet4 and Sheet6 from the clear contents, other worksheets will be cleared:

Code:
Dim Wsheet As Worksheet
For Each Wsheet In Worksheets
    Select Case Wsheet.CodeName
        Case "Sheet2", "Sheet4", "Sheet6"
        Case Else
            Range(Wsheet.Cells(2, 1), Wsheet.Cells(200, 10)).ClearContents
    End Select
Next
 
Upvote 0
Re: VBA select case challenge

What I want the case statement to do is to pick sheets that I want to exclude from the clearing.

That is what the code you posted does. However, you have not qualified your ranges with a worksheet - your code should read:

Code:
Sub ClearRange()
    Dim Wsheet As Worksheet
    For Each Wsheet In Worksheets
    Select Case Wsheet.CodeName
    Case "Sheet2", "Sheet4", "Sheet6"
    Case Else
        WSheet.Range(WSheet.Cells(2, 1), WSheet.Cells(200, 10)).ClearContents
    End Select
    Next
End Sub
 
Upvote 0
Thanks Steve the fish and RoryA .
I will run the codes and let you know what happens.
Thanks again.
Kelly
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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