Clearing Multiple Sheet Range Selections

Brutality

New Member
Joined
Feb 5, 2003
Messages
44
Hi there,

I have the following code working but am wondering if is the safest approach. Obviously all it is doing is clearing the desired selection on the specified worksheets.

*****************

Sub ClearRunSheets()

Dim CodeNameArr, CodNam
Dim ws As Worksheet

CodeNameArr = Array("sShortRun", "sLongRun", "sMedRun", "sNARun")

With ActiveWorkbook
For Each CodNam In CodeNameArr
Set ws = .Worksheets(CStr(.VBProject.VBComponents(CodNam).Properties("Name")))
ws.Range("A2:Z1000") = ""
Next CodNam
End With

End Sub

*****************

I know I could use:

ws.Select
Range("A1:Z1000")
Selection.ClearContents

instead of the single line

ws.Range("A2:Z1000") = ""

to be sure, but setting the cells to empty hasn't given me any problems so far. Is there a reason not to use this approach?

TIA

/*Mark
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Why not just name the range and use a Range("RangeName").Clear command?

Probably wont matter on this scale, but cell by cell loop versus a single range is more efficient.
 
Upvote 0
True enough (there are actually only 7 sheets but I take the point re overhead and best practise). You do mean name the range in the spreadsheet, right? I know it can be done via code but for such a small case that seems more trouble than it's worth.

I just went with
wsh.Range("A2:Z1000").ClearContents

and am still learning the VB basics here.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,673
Members
452,937
Latest member
Bhg1984

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