【2】VBA判斷文件打開否VBA文件打開否 1/VBA語句中關(guān)于判斷文件是否打開的語句 http://www./thread-213721-1-1.html 問:Sub 判斷文件是否已經(jīng)打開() Dim x As Integer For x = 1 To Windows.Count If Windows(x).Caption = "Book2.xls" Then MsgBox "Book2 文件已經(jīng)打開了" Exit Sub End If Next End Sub 可是運(yùn)行了之后卻沒有任何反應(yīng),請教各位老師幫忙看下是那塊編寫錯了嗎? 2# Sub 判斷文件是否已經(jīng)打開() Dim x As Integer For x = 1 To Windows.Count If Windows(x).Caption = "Book2.xls" Then MsgBox "Book2 文件已經(jīng)打開了" Exit Sub Else MsgBox "Book2文件沒有被打開!" End If Next End Sub ----- 2/如何判斷Excel文件是否打開 http://club./thread-1175376-1-1.html 2.1#/Application.GetOpenFilename打開一個Excel文件, 2.2#/2種方法: 2.2#1遍歷WorkBook集合,如集合中存在要打開的工作薄名稱,則表示該工作薄已打開了,否則,表示沒有打開: For Each Wb In WorkBooks If Wb.Name="ABC.xls" Then '如存在,表示已打開 Else '不存在,表示沒打開,則用WorkBook.Open打開此文件 Set Wb1=Workbooks.Open "C:\ABC.xls" End If ... '打開后執(zhí)行的操作語句 Next 2.2#2:On Error 處理,如工作薄沒打開,則會觸發(fā)錯誤則用On Error語句將程序跳轉(zhuǎn)到相應(yīng)語句 On Error GoTo line Set Wb = App1ication.Workbook3 (myWb) MsgBox "工作簿" & myiWb & "已經(jīng)被打開!" Set Wb= Nothing Exit Sub line: MsgBox "工作簿" & myiWb & "未被打開!" 2.5#Application.GetOpen Filename 打開文件地址保存在一個公共變量里,下次使用直接再打開,不管之前是否打開 2.6#/方法1思路不正確,歷遍所有工作簿必須循環(huán)完畢后再做結(jié)論: Sub 判斷工作簿是否已經(jīng)打開方法1() Dim wb As Workbook, bExists As Boolean For Each wb In Workbooks If wb.Name = "ABC.xls" Then '如果存在,表示已經(jīng)打開 bExists = True Exit For End If Next If bExists Then MsgBox "存在" Else MsgBox "不存在" End If End Sub 2.8#如已打開的工作簿不多,可使用1法,否則應(yīng)使用直呼其名法(2樓2法) Sub 判斷工作簿是否已經(jīng)打開方法1() Dim wb As Workbook, bExists As Boolean, f, wn$ f = Application.GetOpenFilename(fileFilter:="Microsoft Excel Files (*.xls;*.xlsx),*.xls;*.xlsx", Title:="選擇Excel文件") If TypeName(f) = "Boolean" Then Exit Sub wn = Split(f, "")(UBound(Split(f, ""))) For Each wb In Workbooks If wb.Name = wn Then '如果存在,表示已經(jīng)打開 bExists = True Exit For End If Next If bExists Then MsgBox "存在" Else MsgBox "不存在" End If End Sub 2.11#/一個無反應(yīng)的宏代碼 --- 3/如何在vba中判斷文件是否已經(jīng)打開 兩種方式。 https://wenda.so.com/q/1531620686213421 3.1通過FileExists判斷文件是否存 Dim MyFile As Object Set MyFile = CreateObject("Scripting.FileSystemObject") If MyFile.FileExists("E:\dzwebs\Pic\logo.gif")=True Then MsgBox"文件存在" Else MsgBox"文件不存在" End If 以上代碼在VBA環(huán)境下使用?;蛘邔⑵渲谱鞒蔀楹暌部梢?。"E:\dzwebs\Pic\logo.gif"為文件路徑及文件名稱,可自行修改。 3.2通過Dir來判斷 Dim YouFile As String YouFile = Dir("E:\MyPictures\Pic\logo.gif")Ifx"" Then MsgBox"文件存在" Else MsgBox "文件不存在" Endif"E:\MyPictures\Pic\logo.gif"為文件路徑及文件名稱,可自行修改。當(dāng)然,如上代碼,也在VBA下使用,可自作成為宏或函數(shù)都可以。上面的兩種方法,本站推薦你使用第一種方法,因?yàn)榈谝环N方法比較符合邏輯思維。 --- 4/Excel 如何判斷,某文件是否已打開?用VBA編寫 http://club./thread-68180-1-1.html 4.4#/樓主的意思應(yīng)該是文件是否打? On Error GoTo 100 Workbooks("BOOK1.XLS").Activate MsgBox "已打開" Exit Sub 100: MsgBox "沒有打開" 4.5#/樓上說的對可以改一下 Sub check_wk() wk = "book1.xls" On Error Resume Next If Len(Workbooks(wk).Name) > 0 Then If Err.Number = 9 Then MsgBox "Can't find " & wk Else MsgBox wk & " is open" End If End If End Sub 4.7#/我用另一種方法實(shí)現(xiàn)某文件是否打開,供參考: sub wkFind() Dim Wb As Workbook For Each Wb In Workbooks If StrComp(Wb.Name,"book1.xLs,1)=0 then Msgbox Wb.Name & "is open." Exit Sub End If Next Msgbox "Can't find book1." End Sub 4.8#你的方法沒有問題,只是StrComp(Wb.Name,"book1.xLs,1)=0有一個筆誤. 也可以用vba.UCase(wk.name)="BOOK1.XLS"判斷 如果工作簿個數(shù)很多的話,你的方法就沒有4樓代碼效率高了. 【】 |
|