Macro for "Clearcontents" button

Bedford

Active Member
Joined
Feb 3, 2015
Messages
328
Office Version
  1. 365
Platform
  1. MacOS
I had a macro assigned to a button in a worksheet that cleared the contents, not formatting in cell ranges; B11:B120, D11:D120, G11:G120, I11:I120, saved it, it worked while I had the workbook open, shutdown, reopened the next day to find it missing and now I can’t recall the code which included deactivating and reactivating the password for the worksheet.

Help.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
That should be very easy to re-create.
If all it does it clear the values in those ranges and saves the workbook, this code should do what you want:
VBA Code:
Sub MyMacro()
    Range("B11:B120, D11:D120, G11:G120, I11:I120").ClearContents
    ActiveWorkbook.Save
End Sub
So then you could just re-attach that code to your button.

EDIT:
Whoops, I missed the part at the end about the password. Kaper shows you how to incorporate that into your code in his post below.
 
Upvote 0
Change your password to right one and in the code below (you may change oprions of protection - I left default set):

VBA Code:
Sub test()
    ActiveSheet.Unprotect Password:="MrExcel"
    Range("B11:B120, D11:D120, G11:G120, I11:I120").ClearContents
    ActiveSheet.Protect Password:="MrExcel", DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
 
Upvote 0
Solution
Change your password to right one and in the code below (you may change oprions of protection - I left default set):

VBA Code:
Sub test()
    ActiveSheet.Unprotect Password:="MrExcel"
    Range("B11:B120, D11:D120, G11:G120, I11:I120").ClearContents
    ActiveSheet.Protect Password:="MrExcel", DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Brilliant, absolutely brilliant. Thank you.
 
Upvote 0
Bedford,

Please Note: In the future, when marking a post as the solution, please mark the post that contains the solution (not your own post acknowledging that some other post was the solution).
When a post is marked as the solution, it is then shown right underneath the original question so people viewing the question can easily see the question and solution in a single quick glance without having to hunt through all the posts.

I have updated this thread for you.
 
Upvote 0

Forum statistics

Threads
1,223,836
Messages
6,174,922
Members
452,592
Latest member
Welshy1491

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