2014-01-23 30 views
0

我有這個代碼到目前爲止在兩個文件之間傳輸。如何檢查目標工作簿是否已打開?我試過IsWorkbookOpen(),但它說「函數未定義」。檢查一個特定的工作簿是否打開

Sub Test2() 

Dim rowCount As Long 
Dim x As Integer 
Dim y As Integer 
Dim z As Integer 
Dim a As Integer 
a = 1 
z = 5 
x = 2 
y = 16 

ActiveWorkbook.Sheets("Results").Activate 
rowCount = Cells(Rows.Count, "B").End(xlUp).Row 

MsgBox "There are " & Workbooks.Count & " open workbooks!" 
For Counter1 = 1 To 8 
a = 1 
z = 5 
MsgBox "From :(" & a & "," & x & ") To:(" & z & "," & y & ")" 


    For Counter = 1 To rowCount 
     If IsWorkbookOpen("CMK & CPK Sheet (Rev2)") = True Then 
      MsgBox "Workbook is Open!" 
     Else 
      MsgBox "Workboook is Not Open!" 

     Workbooks("CMK & CPK Sheet (Rev2)").Sheets(3).Cells(z, y).Value = ActiveWorkbook.ActiveSheet.Cells(a, x).Value 
     z = z + 1 
     a = a + 1 
     Next Counter 
     y = y + 1 
     x = x + 1 

Next Counter1 

End Sub 
+0

是的,沒有這樣的功能。你可以嘗試加里做的事情。另一種方法是在「Object」變量中設置目標工作簿,以測試它是否打開。看我的帖子。 – L42

回答

1

這裏是看是否有特定的工作簿打開一個辦法:

Sub hfskadjrufc() 
    Dim bk As Workbook 
    Dim s As String, ItIsOpen As Boolean 
    s = "todo.xls" 
    ItIsOpen = False 
    For Each bk In Workbooks 
     If bk.Name = s Then 
      ItIsOpen = True 
     End If 
    Next bk 
    MsgBox ItIsOpen 
End Sub 
+0

非常感謝你,這個作品! :d – user3163920

0

試試這個:

Dim targetWB as Workbook 

On Error Resume Next 
Set targetWB = Workbooks("CMK & CPK Sheet (Rev2)") 
On Error Goto 0 

If targetWb Is Nothing Then 
    MsgBox "Workbook not yet open" 
    Exit Sub 'you can terminate procedure or you can use Workbooks.Open 
Else 
    MsgBox "Workbook open" 
    '~~> rest of your code here 
End If 

希望這有助於。

相關問題