我在寫一個VBA程序來檢查文件是否通過返回布爾值的函數存在。但是,我遇到了「File = FileExists(strDataFileName,strDataPath)」行的問題,VBA報告問題是由於類型不匹配造成的。我真的不知道如何解決它。請幫助傢伙。VBA - 調用函數返回布爾值來檢查文件是否存在
p.s.遺憾的代碼也許有點亂,因爲我在VBA編程的業餘
Function FileExists(ByVal sPathName As String, Optional Directory As Boolean) As Boolean
On Error Resume Next
If sPathName <> "" Then
If IsMissing(Directory) Or Directory = False Then
FileExists = (Dir$(sPathName) <> "")
Else
FileExists = (Dir$(sPathName, vbDirectory) <> "")
End If
End If
End Function
Sub AH()
Const strDataPath As String = "C:\Users\"
Dim strFileName As String
Dim strDataFileName As String
Dim File As Boolean
Dim ExistWS as boolean
Dim wbNew As Workbook
strDataFileName = "Past Data"
File = FileExists(strDataFileName, strDataPath)
If File = False Then
Set wbNew = Workbooks.Add
Sheets.Add After:=ActiveSheet
SheetName = Format(Date, "dd-mm-yyyy")
ActiveSheet.Name = SheetName
wbNew.SaveAs Filename:=(strDataPath & strDataFileName), FileFormat:=52
wbNew.Close
Else
Cells(2,3) = 「TRUE」
End If
End Sub
Thz Jimmy!它現在有效! reali thz for yr及時幫助! – 2014-10-10 16:21:32