在networking上打开另一个工作簿需要帮助

你能帮我解决这个问题吗?

我需要在工作簿上运行一些代码(A),以在networking上打开其他工作簿(B,C,D和E)。 而这些其他工作簿也在不断被其他人使用。 所以我没有问题打开这些其他工作簿…如果这些工作簿目前正在被其他人使用它将打开为只读。

我的问题是,如果我有任何这些工作簿(B,C,D和E)在我的电脑上打开。 代码将尝试重新打开这些工作簿,这将触发一条消息说:

“B.xlsm已经打开,重新打开会导致你所做的任何更改被丢弃,你想重新打开B.xlsm吗?

单击是将closures现有的工作簿(B)而不保存并重新打开它。 单击否将popup此运行时错误“1004”:方法“对象工作簿的打开”失败。

如何更改此代码,以便在计算机上打开工作簿(B,C,D和E)(由我打开而不是只读)时,它将继续执行代码而不重新打开它?

你可以天才,请帮我弄明白这一点?

我的代码:

Function IsWorkBookOpen(FileName As String) Dim ff As Long, ErrNo As Long On Error Resume Next ff = FreeFile() Open FileName For Input Lock Read As #ff Close ff ErrNo = Err On Error GoTo 0 Select Case ErrNo Case 0: IsWorkBookOpen = False Case 70: IsWorkBookOpen = True Case Else: Error ErrNo End Select End Function Sub test2() Dim FolderPath As String Dim filePath As String Dim wBook As String FolderPath = Application.ActiveWorkbook.Path filePath = Left(FolderPath, InStrRev(FolderPath, "\") - 1) wBook = filePath & "\Appeals 01.xlsm" 'If Workbook is Opened If IsWorkBookOpen(filePath & "\Appeals 01.xlsm") Then If MsgBox("Appeal 01 is Opened. Do you want to open workbook as Read only?" & vbNewLine & vbNewLine & _ "Warning!!! Running numbers on Read-only mode can cause report not total correctly", vbYesNo, "Already Opened") = vbNo Then Exit Sub Workbooks.Open FileName:=filePath & "\Appeals 01.xlsm" Else Workbooks.Open FileName:=filePath & "\Appeals 01.xlsm" End If MsgBox ("Continue Code") End Sub 

希望你能帮助我…谢谢你们:)

更新:感谢Tbizzness,我已经修改我的代码到这个:

 Function IsWorkBookOpen(FileName As String) Dim ff As Long, ErrNo As Long On Error Resume Next ff = FreeFile() Open FileName For Input Lock Read As #ff Close ff ErrNo = Err On Error GoTo 0 Select Case ErrNo Case 0: IsWorkBookOpen = False Case 70: IsWorkBookOpen = True Case Else: Error ErrNo End Select End Function Sub test2() Dim FolderPath As String Dim filePath As String Dim wBook As String FolderPath = Application.ActiveWorkbook.Path filePath = Left(FolderPath, InStrRev(FolderPath, "\") - 1) wBook = filePath & "\Appeals 01.xlsm" 'Set Boolean to True if it's open on my computer For Each WB1 In Application.Workbooks If WB1.Name = "Appeals 01.xlsm" Then Appeal01bool = True ElseIf WB1.Name = "Appeals 02.xlsm" Then Appeal02bool = True End If Next 'If Appeal 01.xlsm is not open on my computer If Appeal01bool = False Then 'Then is it opened by others If IsWorkBookOpen(filePath & "\Appeals 01.xlsm") Then 'If it is opened by others, do you want to open as Read-only? If MsgBox("Appeal 01 is Opened. Do you want to open workbook as Read only?" & vbNewLine & vbNewLine & _ "Warning!!! Running numbers on Read-only mode can cause report not total correctly", vbYesNo, "Already Opened") = vbNo Then Exit Sub 'Yes to open as read-only Workbooks.Open FileName:=filePath & "\Appeals 01.xlsm" Else Workbooks.Open FileName:=filePath & "\Appeals 01.xlsm" End If 'Save workbbook first if it is opened on this computer Workbooks("Appeals 01.xlsm").Save End If 'If Appeal 02.xlsm is not open on my computer If Appeal02bool = False Then 'Then is it opened by others If IsWorkBookOpen(filePath & "\Appeals 02.xlsm") Then 'If it is opened by others, do you want to open as Read-only? If MsgBox("Appeal 02 is Opened. Do you want to open workbook as Read only?" & vbNewLine & vbNewLine & _ "Warning!!! Running numbers on Read-only mode can cause report not total correctly", vbYesNo, "Already Opened") = vbNo Then Exit Sub 'Yes to open as read-only Workbooks.Open FileName:=filePath & "\Appeals 02.xlsm" Else Workbooks.Open FileName:=filePath & "\Appeals 02.xlsm" End If 'Save workbbook first if it is opened on this computer Workbooks("Appeals 02.xlsm").Save End If MsgBox ("Continue Code") End Sub 

我会用一个简单的方式来检查打开的工作簿的所有标题,如果它打开,则将布尔值设置为true,然后在打开任何工作簿之前检查布尔值:

 for each wb in application.workbooks if wb.name = b then bbool = True elseif wb.name = c then cbool = True elseif wb.name = d then dbool = True elseif wb.name = e then ebool = True end if Next if bbool = false then application.workbooks.open(b) if cbool = false then application.workbooks.open(c) if dbool = false then application.workbooks.open(d) if ebool = false then application.workbooks.open(e)