2014-10-29 40 views
1

你能幫我解決這個問題嗎?在網絡上打開另一個工作簿需要幫助

我需要在工作簿上運行一些代碼(A)以在網絡上打開其他工作簿(B,C,D,& E)。而其他這些工作簿也在不斷被其他人使用。所以我沒有問題打開這些其他工作簿...如果這些工作簿目前正在被其他人使用,它將以只讀方式打開。

我的問題是如果我有任何這些工作簿(B,C,D,& E)在我的電腦上打開。代碼將嘗試重新打開這些工作簿,這將觸發一條消息,內容如下:

「B.xlsm已經打開。重新打開將導致您所做的任何更改被丟棄。是否要重新打開B.xlsm? 「

單擊是將關閉現有工作簿(B)而不保存並重新打開它。 單擊否將彈出此運行時錯誤'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 

希望你能幫助我......謝謝你u人:)

更新:感謝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 

回答

0

我會用一個簡單的外觀檢查打開的工作簿中所有的標題和設置布爾值爲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) 
+0

這對我來說非常合適。謝謝Tbizzness。我是VBA的初學者,今天我剛剛學會了boolean。謝謝。 :) – Tom 2014-11-06 23:08:49