Simple VBA if

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
655
Brainfarting because obviously this isn't right. Ears are open.

THanks

Code:
    If Worksheets("Developer").Range("I46").Value = "'Network" Then        dpathname = cpathname
    Else: dpathname = bpathname
    End If
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Brainfarting because obviously this isn't right. Ears are open.

THanks

Code:
    If Worksheets("Developer").Range("I46").Value = "'Network" Then        dpathname = cpathname
    Else: dpathname = bpathname
    End If
Hard to tell when we don't know what you have or what you are trying to achieve and all we have is code that does not do what you want. ;)

First would be to try this

Code:
If Worksheets("Developer").Range("I46").Value = "'Network" Then        
  dpathname = cpathname
Else
  dpathname = bpathname
End If

If that doesn't solve the issue, we'd need to know more about your objective, what the various variables are and what is actually going wrong with your posted code.
 
Last edited:
Upvote 0
A If Else statement should be written like this:

Code:
Sub My_If()
'Modified  11/5/2019  5:47:50 AM  EST
If Range("A1").Value = "Yes" Then
    MsgBox "Yes"
Else
    MsgBox "No"
End If
End Sub
 
Upvote 0
Sorry- my bad.

Here's a little more for you. It seems odd here to have the a/b/c/d/ etc pathnames but they are utilized individually for file systems and folders later, so necessary. Anyway, I'm blanking on this and it's driving me bananas. But I'm trying to make it such that depending on the value in I46, dpathname will equal either cpathname or bpathname.

Thanks


Code:
    myfilename = path2
    bpathname = Environ("Userprofile") & "\" & path1
    cpathname = Worksheets("Developer").Range("E42") & "\" & path1
    'dpathname = Environ("Userprofile") & "\" & path1
    epathname = dpathname & "\" & Path3
    fpathname = epathname & "\"


    If Worksheets("Developer").Range("I46").Value = "'Network" Then
        dpathname = cpathname
    Else
        dpathname = bpathname
    End If
 
Upvote 0
Are you sure that you require the apostrophe in front of Network?
 
Upvote 0
No I don't. I only put that there because it's not running the dpath section at all (I don't think) and it's " 'Network" in the referenced cell
 
Upvote 0
I did. When I step into the code it shows the appropriate values but when I run the code, it's like it simply skipped over the whole dpathname.

Here's the rest of that portion of the sub:

Code:
Private Sub Save_As()


'Begins Error Handling Code
On Error GoTo Helper


'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 cpathname As String
    Dim dpathname As String
    Dim epathname 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
    bpathname = Environ("Userprofile") & "\" & path1
    cpathname = Worksheets("Developer").Range("E42") & "\" & path1
    'dpathname = Environ("Userprofile") & "\" & path1
    epathname = dpathname & "\" & Path3
    fpathname = epathname & "\"


    If Worksheets("Developer").Range("I46").Value = "Network" Then
        dpathname = cpathname
    Else
        dpathname = bpathname
    End If
    
    ActiveSheet.EnableCalculation = False
     
    If ActiveWorkbook.name = "Master Voyage Report.xlsm" Then
       
        resp = MsgBox("You are trying to save the " & myfilename & " to:" & vbCrLf & fpathname & myfilename & ".xlsm", vbYesNo, name)
        If resp = vbYes Then
            If Len(Dir(fpathname, vbDirectory)) = 0 Then
                If Len(Dir(epathname, vbDirectory)) = 0 Then
                    If Len(Dir(dpathname, vbDirectory)) = 0 Then
                        MkDir (dpathname)
                    End If
                    MkDir (epathname)
                End If
                MkDir (fpathname)
            End If
            ActiveWorkbook.SaveAs Filename:=fpathname & myfilename & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
        '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
    
'Error Clearing Code
Exit Sub
Helper:
    resp = MsgBox("We're sorry to see you've encountered an error." & vbCrLf & vbCrLf & "To proceed, we recommend you contact the Developer " & _
    "with error codes [1147] and " & "[" & Err.Number & "-" & Err.Description & "]." & vbCrLf & vbCrLf & "To attempt to patch your problem at least " & _
    "temporarily, we recommend you click [Yes] to see help directions. Would you like to continue?", vbYesNoCancel, name)
        If resp = vbYes Then
            Call Error_Handle(sprocname, Err.Number, Err.Description)
        ElseIf resp = vbNo Then
            Exit Sub
        ElseIf resp = vbCancel Then
            Exit Sub
        End If
        
End Sub
 
Upvote 0
Please copy and paste in the thread the result that appears in the Immediate window when you run the code below.
Please copy and paste the result do not re-type it.

Code:
Sub chk2()
Debug.Print "| &"; Worksheets("Developer").Range("I46").Value; "& |", Len(Worksheets("Developer").Range("I46").Value)
End Sub

Also if you replace
Code:
    If Worksheets("Developer").Range("I46").Value = "Network" Then
        dpathname = cpathname
    Else
        dpathname = bpathname
    End If
with the code below do you get the message boxes? If so which?

Code:
    If Worksheets("Developer").Range("I46").Value = "Network" Then
        MsgBox "Result1"
    Else
        MsgBox "Result2"
    End If
 
Upvote 0
Sorry for the late reply- I didn't realize my post hadn't gone through on Tuesday....

anyway- interesting- so posting hte msgbox piece was a super simple way to test- I hadn't even thought about that. It worked as intended. The debug piece of coding returned nothing...
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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