Clearing in dividual cells across several worksheets using a button.

Matt5353

Board Regular
Joined
Nov 30, 2018
Messages
60
Office Version
  1. 2016
Platform
  1. Windows
Hi Everyone.
Merry Xmas

I have a spreadsheet that has 5 sheets.
I wish to delete certain cells "only" not rows or columns.
1st row is E5:J5, 2nd G5:J5, 3rd-F5:J5, 4th-G5:J5, 5th-H5:J5, 6th-I5:J5, 7th-J5
I wish to have a button on a Master sheet that will delete all those cells at the same time.
Andrew
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi Matt5353,

Try this:

Code:
Option Explicit
Sub Macro1()

    Dim varMySheet As Variant
    Dim varMyRange As Variant
    Dim lngArrayIndex As Long
    
    'Sheet names to have their cells cleared from.  Change to suit if necessary.
    varMySheet = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6", "Sheet7")
    'Cell ranges to be cleared from the corresponding sheet i.e. Sheet1 will have cells E5:J5 cleared.
    varMyRange = Array("E5:J5", "G5:J5", "F5:J5", "G5:J5", "H5:J5", "I5:J5", "J5")
    
    Application.ScreenUpdating = False
    
    For lngArrayIndex = LBound(varMySheet) To UBound(varMyRange)
        Sheets(varMySheet(lngArrayIndex)).Range(varMyRange(lngArrayIndex)).ClearContents
    Next lngArrayIndex
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Last edited:
Upvote 0
Hi Robert Thanks for that.
? I have tried it but nothing seemed to work.
My sheets have been named would I change the code to refllect that.
Or it would work anyway.
Andrew
 
Upvote 0
Hi Andrew,

Not sure as it worked for me :confused:

You need to have the seven sheet names in the "varMySheet" array you want to clear the contents from via the corresponding range from the "varMyRange" array. So in my code cells G5:J5 would be cleared from worksheet "Sheet2".

If there's no data in the ranges it would like nothing has happened so make sure you have something in the ranges before you run the macro.

Robert
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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