Clearing Named Ranges Clears Entire Sheet

krakken

New Member
Joined
Feb 10, 2018
Messages
3
I have a series of named ranges on a sheet. I want to clear the contents and not the formulas. When I run the below, it clears the named ranges and then clears the entire sheet. Also, if I have no constants, it gives me an error. Help! TIA.

Private Sub CommandButton1_Click()


On Error Resume Next


Range("Inputs_1").Select
Selection.SpecialCells(xlCellTypeConstants).Select
Selection.ClearContents
Range("Inputs_2").Select
Selection.SpecialCells(xlCellTypeConstants).Select
Selection.ClearContents
Range("Inputs_3").Select
Selection.SpecialCells(xlCellTypeConstants).Select
Selection.ClearContents

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this...

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()


[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]


Range("Inputs_1").SpecialCells(xlCellTypeConstants).ClearContents
Range("Inputs_2").SpecialCells(xlCellTypeConstants).ClearContents
Range("Inputs_3").SpecialCells(xlCellTypeConstants).ClearContents


[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

If the named ranges are on a different sheet than the command button, qualify the ranges with the sheet e.g.;
Code:
[B]Sheets("Sheet1").[/B]Range("Inputs_1").SpecialCells(xlCellTypeConstants).ClearContents
 
Last edited:
Upvote 0
When I try that it clears all cells on the excel sheet. Even data outside of the range.


Try this...

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()


[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]


Range("Inputs_1").SpecialCells(xlCellTypeConstants).ClearContents
Range("Inputs_2").SpecialCells(xlCellTypeConstants).ClearContents
Range("Inputs_3").SpecialCells(xlCellTypeConstants).ClearContents


[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

If the named ranges are on a different sheet than the command button, qualify the ranges with the sheet e.g.;
Code:
[B]Sheets("Sheet1").[/B]Range("Inputs_1").SpecialCells(xlCellTypeConstants).ClearContents
 
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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