Macro to clear a specific range of cells from a specific range of worksheets

copperwasher

Board Regular
Joined
Mar 1, 2016
Messages
84
Office Version
  1. 365
Platform
  1. Windows
Hello Top Excel Gurus

I have a workbook containing 36 sheets

Sheets 1 and 2 are summary sheets - that must remain untouched!

sheets 3 to 33 contain pictures (which i have managed to code and add a button to remove them).
The sheets are all differently named too (forex pairs)
But the clearing of data from Rows 1 to 100 is a challenge or the range A1:J100 (Which is the better option)
there are no formulas in the cells, just data which has been pasted in these rows.

My attempt:
Sub ClearContents()
Dim I As Long

For I = 3 To 33
Sheets(CStr(I)).Range("A1:J100").ClearContents
Next I

End Sub

I can see that the sheet reference is not working - i dont know how to specify the worksheets no.3 to no.33


Any help is very much appreciated.
CW
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about
VBA Code:
 Sheets(I).Range("A1:J100").ClearContents
 
Upvote 0
How about
VBA Code:
 Sheets(I).Range("A1:J100").ClearContents

Hey there Mr Fluff

Rather than slap the VBA into the excel doc and continue, i do like to learn and live examples sure are useful...

I get the range: clearcontents, that makes sense...
That is something I have learnt today

Sheet (I)
How will the macro know what sheets to clear and which to leave?
The first two sheets must remain untouched.
The sheets from no.3 to no.33 which are labelled as FX pairs for the ClearContents code.

I do appreciate your help
CW:)(y)
 
Upvote 0
I is the sheet index (the position of the sheet running left to right), so the code will clear the cells on the sheet 3rd from the left, then the 4th from the left, etc.
 
Upvote 0
I is the sheet index (the position of the sheet running left to right), so the code will clear the cells on the sheet 3rd from the left, then the 4th from the left, etc.

Do I just use your code or is it meshed into mine...

I am sorry, I don't follow the sheet index - I am not familiar with VBA code :oops:

Or do I set I = 3 ?
 
Upvote 0
Just replace this line of your code
VBA Code:
 Sheets(CStr(I)).Range("A1:J100").ClearContents
with the code I suggested.
 
Upvote 0
Just replace this line of your code
VBA Code:
 Sheets(CStr(I)).Range("A1:J100").ClearContents
with the code I suggested.

Brilliant Fluff.

Thank you kindly for your work...
it works perfectly...
of course :)(y)(y)
 
Upvote 0

Forum statistics

Threads
1,214,390
Messages
6,119,235
Members
448,879
Latest member
VanGirl

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