【2】VBA判断文件打开否VBA文件打开否 1/VBA语句中关于判断文件是否打开的语句 http://www./thread-213721-1-1.html 问:Sub 判断文件是否已经打开() Dim x As Integer For x = 1 To Windows.Count If Windows(x).Caption = "Book2.xls" Then MsgBox "Book2 文件已经打开了" Exit Sub End If Next End Sub 可是运行了之后却没有任何反应,请教各位老师帮忙看下是那块编写错了吗? 2# Sub 判断文件是否已经打开() Dim x As Integer For x = 1 To Windows.Count If Windows(x).Caption = "Book2.xls" Then MsgBox "Book2 文件已经打开了" 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 ... '打开后执行的操作语句 Next 2.2#2:On Error 处理,如工作薄没打开,则会触发错误则用On Error语句将程序跳转到相应语句 On Error GoTo line Set Wb = App1ication.Workbook3 (myWb) MsgBox "工作簿" & myiWb & "已经被打开!" Set Wb= Nothing Exit Sub line: MsgBox "工作簿" & myiWb & "未被打开!" 2.5#Application.GetOpen Filename 打开文件地址保存在一个公共变量里,下次使用直接再打开,不管之前是否打开 2.6#/方法1思路不正确,历遍所有工作簿必须循环完毕后再做结论: Sub 判断工作簿是否已经打开方法1() Dim wb As Workbook, bExists As Boolean For Each wb In Workbooks If wb.Name = "ABC.xls" Then '如果存在,表示已经打开 bExists = True Exit For End If Next If bExists Then MsgBox "存在" Else MsgBox "不存在" End If End Sub 2.8#如已打开的工作簿不多,可使用1法,否则应使用直呼其名法(2楼2法) Sub 判断工作簿是否已经打开方法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 '如果存在,表示已经打开 bExists = True Exit For End If Next If bExists Then MsgBox "存在" Else MsgBox "不存在" End If End Sub 2.11#/一个无反应的宏代码 --- 3/如何在vba中判断文件是否已经打开 两种方式。 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环境下使用。或者将其制作成为宏也可以。"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"为文件路径及文件名称,可自行修改。当然,如上代码,也在VBA下使用,可自作成为宏或函数都可以。上面的两种方法,本站推荐你使用第一种方法,因为第一种方法比较符合逻辑思维。 --- 4/Excel 如何判断,某文件是否已打开?用VBA编写 http://club./thread-68180-1-1.html 4.4#/楼主的意思应该是文件是否打? 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#/我用另一种方法实现某文件是否打开,供参考: 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"判断 如果工作簿个数很多的话,你的方法就没有4楼代码效率高了. 【】 |
|
来自: c857084163 > 《VBA》