Unlock Cells and Clear Contents

ashani

Active Member
Joined
Mar 14, 2020
Messages
343
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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Post the first macro, but I can't see why it can't be done !!
 
Upvote 0
Thanks @Michael M

Here is the Create PDF Macro - So what I want is when I click on Clear Contents Macro - before clearing, it should check if the PDF macro has been run fully and if not then message box appears to say - Create PDF, if it has then continue as normal.

VBA Code:
Sub CreatePDF_Click()

pdfName = Sheets("Sheet1").Range("C1").Value
ChDir "C:\User"
fileSaveName = Application.GetSaveAsFilename(pdfName, _
fileFilter:="PDF Files (*.pdf), *.pdf")
If fileSaveName <> False Then
Sheets(Array("Sheet1", "2", "3")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fileSaveName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

Sheets("Sheet1").Select
Exit Sub
End If
End Sub


This is the second Macro - so I want this Macro to check before clearing whether the first one has run fully.

Code:
Sub Newday()

Sheets("Sheet1").Select
With ActiveSheet
.Unprotect
     With Range("A1:O40 ")
        .Locked = False
        .ClearContents
End With
.Protect
End With
 
Upvote 0
Maybe this way.
The cells will only be cleared if the PDF has been completed
VBA Code:
Sub CreatePDF_Click()
pdfName = Sheets("Sheet1").Range("C1").Value
ChDir "C:\User"
fileSaveName = Application.GetSaveAsFilename(pdfName, _
fileFilter:="PDF Files (*.pdf), *.pdf")
If fileSaveName <> False Then
Sheets(Array("Sheet1", "2", "3")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fileSaveName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Call NewDay 'this will do the clearing after the PDF has bee created
Exit Sub
End If
End Sub
 
Upvote 0
Thanks @Michael M

but I have 2 command buttons and want to keep it seperate button, because if pdf is created and Clear contents, and later data needs adding amending than it won’t b possible hence seperate buttons.

are there any other alternative to communicate macro 2 to macro 1.

thanks once again
 
Upvote 0
Ok, this will give you a message box if the file HAS been created....UNTESTED
Is that what you need ?
Then you can press the Clearing button !
VBA Code:
Sub CreatePDF_Click()
pdfName = Sheets("Sheet1").Range("C1").Value
ChDir "C:\User"
FileSaveName = Application.GetSaveAsFilename(pdfName, _
fileFilter:="PDF Files (*.pdf), *.pdf")
If FileSaveName <> False Then
Sheets(Array("Sheet1", "2", "3")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FileSaveName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
If Dir(FileSaveName) <> "" Then MsgBox "File exists, Now Press the Clearing Button."
Sheets("Sheet1").Select
Exit Sub
End If
End Sub
 
Upvote 0
Thank you @Michael M

can I have that message / alert box in the second macro if the file hasn’t been created Before clearing contents and abort the process?
 
Upvote 0
I can't test ...I don't have Excel, but try
THIS at the sstart of your create macro...before the SUB line
VBA Code:
Public filesavename As String
Sub CreatePDF_Click()
pdfName = Sheets("Sheet1").Range("C1").Value
ChDir "C:\User"
FileSaveName = Application.GetSaveAsFilename(pdfName, _
fileFilter:="PDF Files (*.pdf), *.pdf")
If FileSaveName <> False Then
Sheets(Array("Sheet1", "2", "3")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FileSaveName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
If Dir(FileSaveName) <> "" Then MsgBox "File exists, Now Press the Clearing Button."
Sheets("Sheet1").Select
Exit Sub
End If
End Sub

THEN
VBA Code:
Sub Newday()
If Dir(filesavename) = "" Then
MsgBox "File has not been created !!!"
Exit Sub
End If
Sheets("Sheet1").Select
With ActiveSheet
.Unprotect
     With Range("A1:O40 ")
        .Locked = False
        .ClearContents
End With
.Protect
End With
End Sub
 
Upvote 0
Thanks @Michael M

Unfortunately it's not working - this line is highlighted "If filesavename <> False Then" with error message runtime error 13 - type mismatch.
 
Upvote 0
Are the macros in the same module ?
Is this line before the create Sub ?
VBA Code:
Public filesavename As String
 
Upvote 0

Forum statistics

Threads
1,214,413
Messages
6,119,374
Members
448,888
Latest member
Arle8907

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