I was hoping someone could help me with my code below. Right now the code stops if the user selects cancel or closes the message box (THIS MESSAGE BOX). I'm trying to have the worksheets protect, workbook protect and hide schedule build and schedule variable tabs if the user selects cancel in the message box or closes the window with X. If they select ok in the message box, then the code should continue as it does now. I've tried a few things, but haven't come up with a solution. I've posted my full code and the section of code I'm trying to fix.
Full code:
Section of code I'm trying to resolve:
Thanks in advance!
Full code:
VBA Code:
Sub PDFSchedule()
Dim result As VbMsgBoxResult
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
ThisWorkbook.Unprotect Password:="password"
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Unprotect Password:="wildcard"
Next ws
Sheets("Schedule variables").Visible = True
Sheets("Schedule build").Visible = True
Sheets("Schedule variables").Select
If Range("F5").Value = "Yes" Then
result = MsgBox("THIS MESSAGE BOX", vbOKCancel)
If result = vbCancel Then MsgBox "Schedule not Produced"
If result = vbCancel Then Exit Sub
End If
Sheets("Schedule variables").Select
If Range("E5").Value = "No" Then
Sheets("Schedule build").Select
Range("SchExecCard").EntireRow.Hidden = True
Sheets("Schedule variables").Select
ElseIf Range("E5").Value = "Yes" Then
Sheets("Schedule build").Select
Range("SchExecCard").EntireRow.Hidden = False
End If
'End of above variable
Sheets("Input Client Info").Select
If Range("V7").Value = "Quarterly" Then
Sheets("Schedule build").Select
Range("SchAnnualCalcDef").EntireRow.Hidden = True
Sheets("Input Client Info").Select
ElseIf Range("V7").Value = "Annual" Then
Sheets("Schedule build").Select
Range("SchAnnualCalcDef").EntireRow.Hidden = False
End If
'End of above variable
Sheets("Schedule variables").Select
If Range("E16").Value = "No" Then
Sheets("Schedule build").Select
Range("SchQuartExistDef").EntireRow.Hidden = True
Sheets("Schedule variables").Select
ElseIf Range("E16").Value = "Yes" Then
Sheets("Schedule build").Select
Range("SchQuartExistDef").EntireRow.Hidden = False
End If
'End of above variable
Sheets("Schedule variables").Select
If Range("E15").Value = "No" Then
Sheets("Schedule build").Select
Range("SchQuartNewDef").EntireRow.Hidden = True
Sheets("Schedule variables").Select
ElseIf Range("E15").Value = "Yes" Then
Sheets("Schedule build").Select
Range("SchQuartNewDef").EntireRow.Hidden = False
End If
'End of above variable
Sheets("Input Client Info").Select
If Range("V7").Value = "Quarterly" Then
Sheets("Schedule build").Select
Range("SchRevShareCalc").EntireRow.Hidden = True
Sheets("Input Client Info").Select
ElseIf Range("V7").Value = "Annual" Then
Sheets("Schedule build").Select
Range("SchRevShareCalc").EntireRow.Hidden = False
End If
'End of above variable
Sheets("Input Client Info").Select
If Range("V7").Value = "Quarterly" Then
Sheets("Schedule build").Select
Range("SchQuartRevShareCalc").EntireRow.Hidden = False
Sheets("Input Client Info").Select
ElseIf Range("V7").Value = "Annual" Then
Sheets("Schedule build").Select
Range("SchQuartRevShareCalc").EntireRow.Hidden = True
End If
'End of above variable
Sheets("Input Client Info").Select
If Range("V8").Value = "No" Then
Sheets("Schedule build").Select
Range("SchUSGridFileTurn").EntireRow.Hidden = True
Sheets("Input Client Info").Select
ElseIf Range("V8").Value = "Yes" Then
Sheets("Schedule build").Select
Range("SchUSGridFileTurn").EntireRow.Hidden = False
End If
Sheets("Input Client Info").Select
If Range("V8").Value = "No" Then
Sheets("Schedule build").Select
Range("SchUSGridCycGrace").EntireRow.Hidden = False
Sheets("Input Client Info").Select
ElseIf Range("V8").Value = "Yes" Then
Sheets("Schedule build").Select
Range("SchUSGridCycGrace").EntireRow.Hidden = True
End If
Sheets("Schedule build").Select
For Each c In Range("SchRebateGridTiers")
If c.Value = "0" Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False
End If
Next
Sheets("Rev Share Grids").Select
If Range("CK30").Value = "No" Then
Sheets("Schedule build").Select
Range("SchGridLTI").EntireRow.Hidden = False
Sheets("Rev Share Grids").Select
ElseIf Range("CK30").Value = "Yes" Then
Sheets("Schedule build").Select
Range("SchGridLTI").EntireRow.Hidden = True
End If
'End of above variable
Sheets("Schedule variables").Select
If Range("F5").Value = "No" Then
Sheets("Schedule build").Select
Range("SchBonusLanguage").EntireRow.Hidden = True
Sheets("Schedule variables").Select
ElseIf Range("F5").Value = "Yes" Then
Sheets("Schedule build").Select
Range("SchBonusLanguage").EntireRow.Hidden = False
End If
'End of above variable
Sheets("Schedule variables").Visible = False
For Each ws In ThisWorkbook.Worksheets
ws.Protect Password:="password"
Next ws
ThisWorkbook.Protect Password:="password"
'Sets schedule build as sheet to export
Set wbA = ActiveWorkbook
Set wsA = Sheets("Schedule build")
strTime = Format(Now(), "yyyymmdd\_hhmm")
'Get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'Replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
'Create default name for saving file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'User can enter name and
' Select folder for file save
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'Export to PDF if a folder was selected
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'Confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
Section of code I'm trying to resolve:
VBA Code:
Sheets("Schedule variables").Select
If Range("F5").Value = "Yes" Then
result = MsgBox("THIS MESSAGE BOX", vbOKCancel)
If result = vbCancel Then MsgBox "Schedule not Produced"
If result = vbCancel Then Exit Sub
End If
Thanks in advance!