从其他Excel文件运行宏时出错

时间:2019-06-26 07:53:48

标签: excel vba

我在一个目录中有两个启用宏的Excel文件。其中之一是SH.xlsm,它具有一个应在我的P.xlsm文件中运行宏的宏。最好,它应该能够在不打开P.xlsm工作簿的情况下运行代码。

这是我目前在SH.xlsm文件中拥有的用于运行P.xlsm中称为InsertImageTest的宏。

Private Sub Worksheet_Change(ByVal Target As Range)
    ' Call InsertImageTest
    codeWorkBookPath = "C:\Users\AsgeirA\OneDrive\Documents\PowerApps\Signature Signer\"
    codeWorkBookName = "P.xlsm"
    codeWorkBookFullPathName = UCase(Trim(codeWorkBookPath)) & UCase(Trim(codeWorkBookName))
    Debug.Print codeWorkBookFullPathName
    codeWorkbookAlreadyOpen = IsWorkBookOpen(codeWorkBookPath & codeWorkBookName)
    If Not codeWorkbookAlreadyOpen Then
        Workbooks.Open FileName:=codeWorkBookFullPathName, UpdateLinks:=False, ReadOnly:=True
    End If

    Workbooks("SH.xlsm").Activate
    Sheets("Data").Activate

    argString = "'" & Trim(codeWorkBookName) & "'!" & Trim("InsertImageTest")
    Application.Run argString
End Sub

Function IsWorkBookOpen(FileName As String)
    Dim ff As Long, ErrNo As Long

    On Error Resume Next
    ff = FreeFile()
    Open FileName For Input Lock Read As #ff
    Close ff
    ErrNo = Err
    On Error GoTo 0

    Select Case ErrNo
    Case 0:    IsWorkBookOpen = False
    Case 70:   IsWorkBookOpen = True
    Case Else: Error ErrNo
    End Select
End Function

我的代码的第一个小问题是它打开了P.xlsm工作簿。第二个问题是未运行“ InsertImageTest”宏。我只是打开第二个工作簿并显示它。

Run-time error '1004':

Cannot run the macro "P.xlsm'!InsertImageTest'. The macro may not be available in this workbook or all macros may be disabled.

1 个答案:

答案 0 :(得分:0)

我已经做过一些调查,看来新版本的Microsoft(16.0.11727.20188)引起了问题。我可以建议降级到较旧的版本,以解决该问题。