根据文件名取消sub的if语句

If Statement That Cancels Sub Based on File Name

我正在尝试执行 and If 语句,如果它为真,则显示一个消息框并且不执行子操作,如果为真,则子操作正常执行。

使用我当前的代码,即使应该满足 If 条件,sub 总是被执行。我不确定哪里出错了:

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

所有其他代码都在 else 之下,并以 End If 结尾。

Post 根据以下评论中的讨论接受后编辑。


添加了 Option Explicit 并指定了如何列出文件路径,这应该与以下内容类似:

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