如何使用VBA自动接受Excel提示?

时间:2016-10-26 05:52:30

标签: excel vba excel-vba

我有一个VBA子程序,用于检查工作簿是否已打开,然后将其他工作簿中的数据复制并粘贴到其中。这是在application.ontime循环中自动完成的。我通常会在处理其他事情时将其保持最小化,并偶尔检查Excel数据副本/粘贴。 Excel会不时弹出一个框,说明目标工作簿已打开,并询问我是否要重新打开它。弹出的时间和我检查工作簿的时间之间错过了许多子运行。

问题出现的代码:

 override func draw(_ rect: CGRect) {
    let skullRadius = min(bounds.width, bounds.height)
    let skullCenter = CGPoint(x: bounds.midX, y: bounds.midY)
    let skull = UIBezierPath(arcCenter: skullCenter, radius: skullRadius, startAngle: 0.0, endAngle: CGFloat(2*M_PI), clockwise: false)
    skull.lineWidth = 3
    UIColor.blueColor().setFill()
    skull.stroke()
}

如何让Excel自动处理这种情况?

次要问题:我怀疑它可能与我在运行之间触摸工作簿而不保存它的事实有关,导致Excel认为自上次保存以来有变化。如果这可能是一个原因,有人可以评论吗?

1 个答案:

答案 0 :(得分:2)

首先检查工作簿是否已打开。如果没有打开尝试打开它。这可以在下面的代码中完成。请参阅link

Sub TestFileOpened()
    Dim fullPath As String

    fullPath = "C:\Df.xlsx"
    ' Test to see if the file is open.
    If IsFileOpen(fullPath) Then
      Set dataset = Workbooks("Df.xlsx")
    Else
        Set dataset = Workbooks.Open(fullPath)
    End If

End Sub

Function IsFileOpen(filename As String)
    Dim filenum As Integer, errnum As Integer

    On Error Resume Next   ' Turn error checking off.
    filenum = FreeFile()   ' Get a free file number.
    ' Attempt to open the file and lock it.
    Open filename For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.
    On Error GoTo 0        ' Turn error checking back on.

    ' Check to see which error occurred.
    Select Case errnum

        ' No error occurred.
        ' File is NOT already open by another user.
        Case 0
         IsFileOpen = False

        ' Error number for "Permission Denied."
        ' File is already opened by another user.
        Case 70
            IsFileOpen = True

        ' Another error occurred.
        Case Else
            Error errnum
    End Select

End Function