2024年2月10日发(作者:)

在 VBA 中,`FileExists` 函数用于检查指定的文件是否存在。它的语法如下:

```vba

FileExists(filename As String) As Boolean

```

其中,`filename` 是要检查的文件路径和文件名。如果该文件存在,则返回 `True`,否则返回 `False`。

以下是一个使用 `FileExists` 函数的示例:

```vba

Sub CheckFileExists()

Dim filePath As String

Dim exists As Boolean

filePath = "C:"

exists = FileExists(filePath)

If exists Then

MsgBox "文件存在"

Else

MsgBox "文件不存在"

End If

End Sub

```

在上面的示例中,我们定义了一个名为 `filePath` 的字符串变量,它包含要检查的文件的路径和文件名。然后,我们使用 `FileExists` 函数检查该文件是否存在,将结果存储在名为 `exists` 的布尔变量中。最后,根据 `exists` 的值,我们显示相应的消息框。