Missing an if issue

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
655
I messed up an if here and was hoping someone could check it for me

Code:
Private Sub Save_As()'Creates the SaveAs "Current Voyage" on the Noon Sheet


    Dim Path1 As String
    Dim Path2 As String
    Dim Path3 As String
    Dim myfilename As String
    Dim fpathname As String
    Dim resp As Integer
    Dim name As String
    
    With Worksheets("Notes")
    Path1 = .Range("O16")
    name = .Range("N4")
    Path2 = .Range("O18")
    Path3 = .Range("U16")
    End With
    
    myfilename = Path2
    fpathname = Environ("Userprofile") & "\" & Path1 & "\" & Path3 & "\"
    
    If ActiveWorkbook.name = "Master Voyage Report.xlsm" Then
        ActiveSheet.EnableCalculation = False
    
        resp = MsgBox("You are trying to save the " & myfilename & " to:" & vbCrLf & fpathname & myfilename & ".xlsm", vbYesNo, name)
        If resp = vbYes Then
            If Dir(fpathname, vbDirectory) = "" Then MkDir (fpathname)
                Else: ActiveWorkbook.SaveAs Filename:=fpathname & myfilename & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
            End If
                'Application Closer
            If Workbooks.Count > 1 Then
                ActiveWorkbook.Close
            Else: Application.Quit
            End If
        ElseIf vbNo Then
            Exit Sub
        ElseIf vbCancel Then
            Exit Sub
        End If
    ElseIf ActiveWorkbook.name = myfilename Then
        ActiveWorkbook.Save
        'Application Closer
                If Workbooks.Count > 1 Then
                    ActiveWorkbook.Close
                Else: Application.Quit
                End If
    Else: ActiveWorkbook.Save
        'Application Closer
                If Workbooks.Count > 1 Then
                    ActiveWorkbook.Close
                Else: Application.Quit
                End If
    End If
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
This art is wrong
Code:
            If Dir(fpathname, vbDirectory) = "" Then MkDir (fpathname)
                Else: ActiveWorkbook.SaveAs Filename:=fpathname & myfilename & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
            End If
You are mixing a one line if & a block if
Move the MkDir line to a line of it's own
 
Upvote 0
perfect!

So if I put an "If xxx Then xxx" I don't need an "End If" right?

and If I have an "Else:" I do still need an "End If" correct?

Finally, with "ifs", If I want it to check multiple criteria and have multiple outcomes, like
"if A1 = Yes, then yes"
"elseif A2 = Yes, then yes"

or is elseif contigent on the first if being true/false?

Does that make sense? Basically check each criteria and do each outcome separately (I know I can do this with multiple Ifs and End Ifs) but I want them share a common msgbox at the end?

Basically how to condense this:
Code below:

Code:
If Sheets("Notes").Range("M30").Value = "Yes" Then        Call Printer
    End If
    If Sheets("Developer").Range("D10").Value = "Yes" Then
        Call Email_Active_Sheet
    End If
    If Sheets("Notes").Range("D12").Value = "Yes" Then
        Call EmailActiveMaster
    End If
    If Sheets("Developer").Range("B12").Value = "Yes" Then
            Call EmailActiveEngineers
    End If
    MsgBox "The form has been emailed and should be coming out of the printer momentarily.", vbOKOnly, name
 
Upvote 0
You could write that If like
Code:
If Dir(fpathname, vbDirectory) = "" Then MkDir (fpathname) Else ActiveWorkbook.SaveAs Filename:=fpathname & myfilename & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
or like
Code:
If Dir(fpathname, vbDirectory) = "" Then
   MkDir (fpathname)
Else
   ActiveWorkbook.SaveAs Filename:=fpathname & myfilename & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
End If
They do the same thing, but you cannot mix them
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Of course!

One other question

If I want to check that a directory exists, I know
Code:
IF Len(Dir(xxx)) = "" Then
works but can I use
Code:
IF Len(Dir(xxx)) = 0 Then
or will that zero kick an error?

It seems in both cases I've kicked errors before....but I can never understand one over the other
 
Upvote 0
Len returns a number so you have to check for =0
 
Upvote 0
It's the other way round
Len=0
Dir=""
Not sure if there is any benefit to one method over the other.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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