Clear Contents of specific worksheets and ranges

Learn_VBA

New Member
Joined
Oct 9, 2017
Messages
25
Sub Clear()
Application.DisplayAlerts = True
Dim IA As Workbook:
Dim ES As Worksheet
Dim InputSheet As Worksheet
Dim Competition As Worksheet
Dim Demographics As Worksheet
Dim AlteryxOutput As Worksheet
Dim Impact As Range
Set IA = ThisWorkbook
Set ES = IA.Sheets("Executive Summary")
Set Competiton = IA.Sheets("Competition")
Set AlteryxOutput = IA.Sheets("Alteryx Output")
Set InputSheet = IA.Sheets("Input Sheet")
Set InputValues = ES.Range("B1:B14")
Set Impact = ES.Range("L11:M13")
InputSheet.ClearContents
Competition.ClearContents
AlteryxOutput.ClearContents
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
A few things:

- You have a typo:
Code:
[COLOR=#333333]Set [/COLOR][COLOR=#ff0000]Competiton[/COLOR][COLOR=#333333] = IA.Sheets("Competition")[/COLOR]
If you use "Option Explicit" at the top of all your modules, it will catch these kind of typos and alert you

- You have not declared "InputValues"

- I think you may need to Activate each sheet before you clear its contents

- I am not sure why you are setting ranges when you aren't using them at all in this procedure.

Here is a version of your code that clears all contents on those three tabs:
Code:
Sub MyClear()
    
    Application.DisplayAlerts = True

    Dim IA As Workbook:
    Dim ES As Worksheet
    Dim InputSheet As Worksheet
    Dim Competition As Worksheet
    Dim Demographics As Worksheet
    Dim AlteryxOutput As Worksheet
    Dim InputValues As Range
    Dim Impact As Range
    
    Set IA = ThisWorkbook
    Set ES = IA.Sheets("Executive Summary")
    Set Competition = IA.Sheets("Competition")
    Set AlteryxOutput = IA.Sheets("Alteryx Output")
    Set InputSheet = IA.Sheets("Input Sheet")
    
    Set InputValues = ES.Range("B1:B14")
    Set Impact = ES.Range("L11:M13")
    
    InputSheet.Activate
    Cells.ClearContents
    
    Competition.Activate
    Cells.ClearContents
    
    AlteryxOutput.Activate
    Cells.ClearContents
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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