亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用VBA檢查文件是否存在

使用VBA檢查文件是否存在

慕桂英4014372 2019-10-30 10:26:25
Sub test()thesentence = InputBox("Type the filename with full extension", "Raw Data File")Range("A1").Value = thesentenceIf Dir("thesentence") <> "" Then    MsgBox "File exists."Else    MsgBox "File doesn't exist."End IfEnd Sub在這種情況下,當我從輸入框中提取文本值時,它不起作用。但是,如果"the sentence"從If中刪除Dir()并將其替換為代碼中的實際名稱,則它可以工作。有人可以幫忙嗎?
查看完整描述

3 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

請注意,您的代碼Dir("thesentence")應包含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


查看完整回答
反對 回復 2019-10-30
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

從@UberNubIsTrue對fileExists的更正:


Function fileExists(s_directory As String, s_fileName As String) As Boolean


  Dim obj_fso As Object, obj_dir As Object, obj_file As Object

  Dim ret As Boolean

   Set obj_fso = CreateObject("Scripting.FileSystemObject")

   Set obj_dir = obj_fso.GetFolder(s_directory)

   ret = False

   For Each obj_file In obj_dir.Files

     If obj_fso.fileExists(s_directory & "\" & s_fileName) = True Then

        ret = True

        Exit For

      End If

   Next


   Set obj_fso = Nothing

   Set obj_dir = Nothing

   fileExists = ret


 End Function

編輯:縮短版本


' Check if a file exists

Function fileExists(s_directory As String, s_fileName As String) As Boolean


    Dim obj_fso As Object


    Set obj_fso = CreateObject("Scripting.FileSystemObject")

    fileExists = obj_fso.fileExists(s_directory & "\" & s_fileName)


End Function


查看完整回答
反對 回復 2019-10-30
  • 3 回答
  • 0 關注
  • 840 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號