当从第三个工作簿本身打开的工作簿调用打开时,工作簿不会打开

时间:2017-03-24 18:40:04

标签: excel vba macos excel-vba

我在vba上面临一个问题,要解释一下我自己,从一个文件(让它称之为file1),点击一个按钮,我打开另一个工作簿(file2),当到达这个工作簿时,它会问打开另一个(file3),但它没有....(当我写"设置WBK =工作簿(file3)时,它给出了错误"下标超出范围" )

实际上,当我直接打开file2时,它会打开文件3而没有任何问题

有人对此有答案吗?

IN FILE 1

Private Sub CommandButton3_Click()

        UserForm1.Hide

        Workbooks.Open ("Boss4 copy.xlsm")

End Sub

在文件2中(Boss4副本)

Private Sub Workbook_Open()
Dim MyPath As String
Dim MyScript As String
Dim MyFiles As String
Dim MySplit As Variant
Dim N As Long
Dim Fname As String
Dim mybook As Workbook


On Error Resume Next
MyPath = MacScript("return (path to documents folder) as String")

MyScript = _
"set applescript's text item delimiters to "","" " & vbNewLine & _
           "set theFiles to (choose file of type " & _
         " {""com.microsoft.Excel.xls"",""org.openxmlformats.spreadsheetml.sheet""} " & _
           "with prompt ""Please select a file or files"" default location alias """ & _
           MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
           "set applescript's text item delimiters to """" " & vbNewLine & _
           "return theFiles"

MyFiles = MacScript(MyScript)
On Error GoTo 0

    MySplit = Split(MyFiles, ",")
    For N = LBound(MySplit) To UBound(MySplit)

        ' Get the file name only and test to see if it is open.
        Fname = Right(MySplit(N), Len(MySplit(N)) - InStrRev(MySplit(N), Application.PathSeparator, , 1))
        If bIsBookOpen(Fname) = False Then

            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(MySplit(N))
            On Error GoTo 0
        Else
            MsgBox "the file: " & MySplit(N) & " is already opened."
        End If

    Next N
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

    **Here's the error "subscript out of range"**
    Set WBK = Workbooks(Fname)

    Else: Exit Sub

End If

End Sub

仍然在文件2

Function bIsBookOpen(ByRef szBookName As String) As Boolean
    On Error Resume Next
    bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function

0 个答案:

没有答案