如果工作簿已经打开,请继续

时间:2018-05-17 15:21:28

标签: excel vba excel-vba

您好我正在制作"包装清单表格(PL)"有一组代码打开一个"跟踪表格",并将那里的任何内容复制到"包装清单表格(PL)"

但是当我第二次运行代码时," Tracing Form"已经开放了, 所以它提示我说

""追踪表格"已经开放了。重新打开将导致您所做的任何更改被丢弃。你想重新打开"追踪表格"?"

我希望它说不,继续其余的代码!

请帮忙...... 这是相关代码的一部分

Set PL = Workbooks("PACKING LIST FORM").ActiveSheet    
userprofile = Environ$("userprofile")

Workbooks.Open userprofile & "\Dropbox\Tissue Tracing Form"

Set tracingform = Workbooks("Tissue Tracing Form.xlsx").Worksheets("2018_1")
ActiveWindow.WindowState = xlMinimized 

'execution
from_lastrow = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).row
.
.
.
.

1 个答案:

答案 0 :(得分:1)

下面的函数允许您检查工作簿是否已打开并为xRet分配一个布尔值,然后您可以在案例中使用它来运行其余代码或打开工作簿,然后运行其余代码。这将避免您在工作簿已经打开时尝试打开它,因此您不会收到弹出消息。

Sub CheckExcelOpen()

Dim myPath As String
Dim xRet As Boolean

    xRet = IsWorkBookOpen(Tissue Tracing Form.xlsx)
    If xRet Then
        'YOUR CODE WHEN ALREADY OPEN
    Else
        Workbooks.Open (TissueTracingFullFilePath) 'action needs the full file path to open
       'YOUR CODE ONCE OPEN
    End If

End Sub

功能 -

Function IsWorkBookOpen(Name As String) As Boolean
    Dim xWb As Workbook
    On Error Resume Next
    Set xWb = Application.Workbooks.Item(Name)
    IsWorkBookOpen = (Not xWb Is Nothing)
End Function

我希望这会有所帮助