检查特定工作簿是否已打开

时间:2014-01-23 00:49:30

标签: excel-vba vba excel

到目前为止,我有这个代码在两个文件之间传输。如何检查目标工作簿是否已打开?我已经尝试过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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

希望这有帮助。