Unlock Cells and Clear Contents

ashani

Active Member
Joined
Mar 14, 2020
Messages
345
Office Version
  1. 365
Platform
  1. Windows
Hi
I'm trying to set the macro up to unlock multiple ranges and clear contents on those ranges from Sheet1,Sheet2 & Sheet3 and also to set the message box to say "xyz" - please can someone guide me.

Many thanks,
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
What ranges do you want cleared ?
Have you tried using the macro recorder to achieve this ?
 
Upvote 0
Hi Michael,

I have tried that but it's not working so I want to clear for example : Sheet1 (A4:B5, C10:D20), Sheet2 (A7:Q27) and so on.

Many thanks
 
Upvote 0
Try
VBA Code:
Sub MM1()
With Sheets("Sheet1")
.Range("A4:B5", "C10:D20").ClearContents
End With
With Sheets("Sheet2")
.Range("A7:Q27").ClearContents
End With
MsgBox "All cells cleared ! "
End Sub
 
Upvote 0
Thanks so much Michael
How about unlocking those cells in the range so the cells are set to auto lock after data entry and I want to clear contents as well as unlock those cells for future entries.
 
Upvote 0
VBA Code:
Option Explicit

Sub MM1()
With Sheets("Sheet1")
.Unprotect
    With Range("A4:B5", "C10:D20")
        .Locked = False
        .ClearContents
        .Locked = True
    End With
.Protect
End With
Sheets("Sheet2").Select
With Sheets("Sheet2")
.Unprotect
    With Range("A7:Q27")
        .Locked = False
        .ClearContents
        .Locked = True
    End With
.Protect
End With
End Sub
 
Upvote 0
Thanks Michael
I'm getting error message of Compile Error, Wrong number of arguments or invalid property assignment

The range is highlighted and this is what I have put

With Range("B8:C13", "E8:G13", "F25:G29", "F31:G35")
 
Upvote 0
VBA Code:
Sub MM1()
Sheets("Sheet1").Select
With ActiveSheet
.Unprotect
     With Range("B8:C13, E8:G13, F25:G29, F31:G35")
        .Locked = False
        .ClearContents
        .Locked = True
    End With
.Protect
End With
Sheets("Sheet2").Select
With ActiveSheet
.Unprotect
    With Range("A7:Q27")
        .Locked = False
        .ClearContents
        .Locked = True
    End With
.Protect
End With
End Sub
 
Upvote 0
You're amazing Michael - thank you every so much. I owe you big time :)
 
Upvote 0
Hi Michael
Sorry to bother you again - quick question :

In my spreadsheet there are 2 macros.

1) to create pdf and save - call “create pdf”
2) this one to clear contents and unlocked

Is it possible to have an option that if the first macro hasn’t been run fully (I.e created pdf) then the message appears “you need to save file” before it clears the cell ?

Thank you once again
 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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