Activex command button to clear contents

sloany101

New Member
Joined
Jan 7, 2023
Messages
35
Office Version
  1. 365
I need some help , i feel i am very close , i have 31 sheets and have multiple separate ranges of cells i am looking to clear with an activex command button.

Below is a line of code that actually works very well.

Private Sub CommandButton1_Click()
If MsgBox("Are you sure? Cannot undo.", vbYesNo) = vbNo Then Exit Sub
'Updateby Extendoffice

Sheet2.Range("B7:T18").ClearContents
Sheet2.Range("B21:T32").ClearContents
Sheet2.Range("B40:T51").ClearContents
Sheet2.Range("B54:T65").ClearContents
Sheet2.Range("B73:T84").ClearContents
Sheet2.Range("B87:T98").ClearContents
Sheet2.Range("B107:T118").ClearContents
Sheet2.Range("B121:T132").ClearContents

Sheet3.Range("B7:T18").ClearContents
Sheet3.Range("B21:T32").ClearContents
Sheet3.Range("B40:T51").ClearContents
Sheet3.Range("B54:T65").ClearContents
Sheet3.Range("B73:T84").ClearContents
Sheet3.Range("B87:T98").ClearContents
Sheet3.Range("B107:T118").ClearContents
Sheet3.Range("B121:T132").ClearContents
End Sub

BUT , what i want to do is for each sheet's array of cells (IE: B7:T18 , B21:T32, ETC) consolidate them to one line of code (instead of 8 lines of code for each sheet), to something like this

Private Sub CommandButton1_Click()
If MsgBox("Are you sure? Cannot undo.", vbYesNo) = vbNo Then Exit Sub
'Updateby Extendoffice
Sheets("Sheet1").Array.Range("B7:T18", "B21:T32", "B40:T51", "B54:T65", "B73:T84", "B87:T98", "B107:T118", "B121:T132").ClearContents
Sheets("Sheet2").Array.Range("B7:T18", "B21:T32", "B40:T51", "B54:T65", "B73:T84", "B87:T98", "B107:T118", "B121:T132").ClearContents
ETC
End Sub

If anyone can help me out i would greatly appreciate it , i feel i am close , ALSO note the above consolidated code when used states "Run-time error '9': Subscript out of range"
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try it like this...

VBA Code:
Sheet2.Range("B7:T18,B21:T32,B40:T51,B54:T65,B73:T84,B87:T98,B107:T118,B121:T132").ClearContents

Actually, it looks like your sheets are named Sheet1, Sheet2, . . . Sheet31. If so, you can re-write your code as follows...

VBA Code:
    Dim i As Long
    For i = 1 To 31
        ThisWorkbook.Worksheets("Sheet" & i).Range("B7:T18,B21:T32,B40:T51,B54:T65,B73:T84,B87:T98,B107:T118,B121:T132").ClearContents
    Next i

Hope this helps!
 
Upvote 0
Solution
Try it like this...

VBA Code:
Sheet2.Range("B7:T18,B21:T32,B40:T51,B54:T65,B73:T84,B87:T98,B107:T118,B121:T132").ClearContents

Actually, it looks like your sheets are named Sheet1, Sheet2, . . . Sheet31. If so, you can re-write your code as follows...

VBA Code:
    Dim i As Long
    For i = 1 To 31
        ThisWorkbook.Worksheets("Sheet" & i).Range("B7:T18,B21:T32,B40:T51,B54:T65,B73:T84,B87:T98,B107:T118,B121:T132").ClearContents
    Next i

Hope this helps!
The first one worked perfectly , tbh i tried the 2nd one once and it wasnt working , but im ok with using the first one , it works fine , thank you very much !!
 
Upvote 0
You're very welcome, I'm glad I could help.

Although, if you confirm how your sheets are named, I can easily provide you with the correct code.

Cheers!
 
Upvote 0
You're very welcome, I'm glad I could help.

Although, if you confirm how your sheets are named, I can easily provide you with the correct code.

Cheers!
the sheets are named as you guessed its basically 1-31 , its a spreadsheet used for entire months that we reuse every month and instead of having to go in and manually clear all the cells was looking to streamline the process , and your code worked well and helped me cut a lot of time out in writing it thanks again .
 
Upvote 0
Just to be clear, is it 1, 2, 3, ... 31 ? Or Sheet1, Sheet2, Sheet3, ... Sheet31?

Also, we're talking sheet names, not code names, right?
 
Upvote 0
Then the code should work, unless . . .

1) One or more sheets do not exist, in which case you'll get a subscript out of range error.

2) One or more sheets are protected, and the target cells are locked, in which case you'll get a cells are protected error.

3) The sheets do not exist in the workbook running the code, but rather the active workbook, in which case you can use the following instead...

VBA Code:
ActiveWorkbook.Worksheets("Sheet" & i).Range("B7:T18,B21:T32,B40:T51,B54:T65,B73:T84,B87:T98,B107:T118,B121:T132").ClearContents

If you continue to have problems, please clarify. And, if you get an error, please confirm which line gives you the error, and the type of error it gives you.
 
Upvote 0

Forum statistics

Threads
1,224,795
Messages
6,180,995
Members
453,011
Latest member
Osamu9Dazai

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