If statement and Message box

bcf0123

New Member
Joined
May 13, 2020
Messages
9
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
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:
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!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Didn't see how I could edit the post. I forgot to mention ending the code. So if the user selects cancel or closes the message box then I'd like the things below to happen in order.
  1. Hide schedule build and schedule variables tabs
  2. Protect all worksheets and workbook with "password"
  3. Code ends
 
Upvote 0
I tired a few more things earlier today but still can't get it to work. Anyone got ideas?
 
Upvote 0

Forum statistics

Threads
1,223,945
Messages
6,175,555
Members
452,652
Latest member
eduedu

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