부울을 반환하는 함수를 통해 파일이 있는지 확인하기 위해 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 지미! 이제 작동합니다! 적기에 도움을 주신 reali thz! –