Macro should only work if other macros has run fully

ashani

Active Member
Joined
Mar 14, 2020
Messages
345
Office Version
  1. 365
Platform
  1. Windows
Hi experts,
i wonder if someone can help me on this please :

I have 2 command buttons in my spreadsheet
1. Create PDF
2. Clear Contents

What I want that clear contents macro should only work if the create PDF macro has run fully othrwise message box come up to say Create PDF first.

Please can someone guide me.

thank you

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


Code:
Sub Newday()

Sheets("Sheet1").Select
With ActiveSheet
.Unprotect
     With Range("A1:O40 ")
        .Locked = False
        .ClearContents
End With
.Protect
End With
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
add these few lines
VBA Code:
public pdfrun As Boolean

Sub CreatePDF_Click()
pdfrun = True
''' your code here
End Sub

Sub Newday()
If pdfrun Then
Sheets("Sheet1").Select
With ActiveSheet
.Unprotect
     With Range("A1:O40 ")
        .Locked = False
        .ClearContents
End With
.Protect
End With
Else
MsgBox ("Please create the PDF first")
End If
End Sub
to your code:
 
Upvote 0
Thank you sir

The code works perfectly but every time i close and reopen the file it says the same thing. Is it possible that once saved on the location then it works normally ?

Thank you once again.
 
Upvote 0
Add this macro to your current module:
VBA Code:
Sub clearpdf()
pdfrun = False

End Sub
and then add this to the workbook code open event:
Code:
Private Sub Workbook_Open()
Call clearpdf

End Sub
 
Upvote 0
Hi Thank you so much for all your help. Is it possible to put something in the pdf code below so if there is any value in cell F33 than it wouldn't allow the user to close excel file without creating a PDF. That way then it will push user to create PDF file before clearing it.

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
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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