Search code examples
excelvbaexcel-2016

If Statement That Cancels Sub Based on File Name


I am trying to implement and If statement where if it is true, a message box is displayed and the sub is not carried out and if it is true, the sub is carried out like normal.

With my current code, the sub is always carried out, even though the If condition should be met. I am not sure where I have gone wrong:

Dim templateFile As String
templateFile = "T:\Sales and Subs Dept\ARK - Dev\ALPHA TEMPLATE w.o Loop.xlsm"

If Filepath = templateFile Then
    MsgBox "Please save template as different document"
    Exit Sub
Else

All other code goes below the else and concludes with an End If.


Solution

  • Post edited after accepting per the discussion in the comments below.


    Added Option Explicit and specified how to list the Filepath, which should keep things similar:

    Option Explicit
    sub fdsa()
        Dim templateFile As String, Filepath As String
        Filepath = Application.ActiveWorkbook.path & ActiveWorkbook.Name & ".xlsm"
        templateFile = "T:\Sales and Subs Dept\ARK - Dev\ALPHA TEMPLATE w.o Loop.xlsm"
        If Filepath = templateFile Then 
            MsgBox "Please save template as different document."
            Exit Sub
        Else
            'run your macro where case isn't true
        End If
    end sub