使用 VBA 检查文件是否存在 [英] Check if the file exists using VBA

查看:27
本文介绍了使用 VBA 检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sub test()

thesentence = InputBox("Type the filename with full extension", "Raw Data File")

Range("A1").Value = thesentence

If Dir("thesentence") <> "" Then
    MsgBox "File exists."
Else
    MsgBox "File doesn't exist."
End If

End Sub

在这里,当我从输入框中提取文本值时,它不起作用.但是,如果从 If Dir() 中删除 "the sentence" 并将其替换为代码中的实际名称,则它可以工作.有人可以帮忙吗?

In this when i pickup the text value from the input box, it doesn't work. If however, if remove "the sentence" from If Dir() and replace it with an actual name in the code, it works. Can somebody help?

推荐答案

注意你的代码包含 Dir("thesentence") 应该是 Dir(thesentence).

Note your code contains Dir("thesentence") which should be Dir(thesentence).

将您的代码更改为此

Sub test()

thesentence = InputBox("Type the filename with full extension", "Raw Data File")

Range("A1").Value = thesentence

If Dir(thesentence) <> "" Then
    MsgBox "File exists."
Else
    MsgBox "File doesn't exist."
End If

End Sub

这篇关于使用 VBA 检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆