在Excel VBA中使用variablessearch文件名

我有代码应该search服务器上的文件夹中的文件名,如果该文件存在提示用户覆盖文件或不。 但是,代码总是提示用户,而不pipe文件是否存在。

我发现这个网站的一些例子,但是他们使用静态文件名。 我试图修改它以适应我的需要,但没有得到任何地方。

查找文件是否存在的代码如下

'checks if file you're saving already exists If FileThere("\\showdog\service\Service_job_PO\" & UserInput) = True Then 'do stuff here 

该代码的function是这样的:

 Function FileThere(filename As String) As Boolean If FileThere = (Dir("\\showdog\service\Service_job_PO\" & UserInput) <> "") Then FileThere = True Else FileThere = False End If End Function 

我相信这应该是下面。 您想要检查与传递的参数filename关联的目录是否存在。

 Function FileThere(filename As String) As Boolean If Dir(filename) <> "" Then FileThere = True Else FileThere = False End If End Function 

编辑:

我假设你在这一行中提供完整的文件path,在这种情况下,你引用了发送到函数FileThere的文件path。

  If FileThere("\\showdog\service\Service_job_PO\" & UserInput) = True Then 

你可以把这一切都削减到一个单一的testing,而不是用UDF加倍:

 If Len(Dir("\\showdog\service\Service_job_PO\" & UserInput)) > 0 Then 

根据上面的其他注释,您的初始代码是两次使用UserInput