我的问题有点奇怪...我在Access VBA中有代码。我想在其他Excel文件中运行两个宏。第一个宏运行没有问题,但是当dubbger获得第二个时,我得到一个运行时错误1004:“无法运行宏'prcPrepareFirstReport'。这个worbook中没有宏可用,或者所有宏都被禁用。”
访问代码:
Private Sub cmdRaport_Click()
Dim pPath As String
Dim pWorkPath As String
Dim pExcel As Object
Dim pPathToSave As String
Dim pTargetPath As String
pPath = fncFilePicker()
pWorkPath = DLookup("[F_LINK]", "tblLinks", "[F_ID] = 1")
Set pExcel = CreateObject("Excel.Application")
pExcel.Workbooks.Open (pWorkPath)
pMacro = "prcPrepareFile"
pPathToSave = DLookup("[F_LINK]", "tblLinks", "[F_ID] = 2")
pExcel.Run pMacro, pPath, pPathToSave 'this macro is going fine
pMacro = "prcPrepareFirstReport"
pTargetPath = DLookup("[F_LINK]", "tblLinks", "[F_ID] = 5")
pExcel.Run pMacro, pTargetPath 'on this macro I'm given error 1004
pExcel.ActiveWorkbook.Close
pExcel.Quit
Set pExcel = Nothing
End Sub
Excel VBA代码:
Public pSourceName As String
Sub prcPrepareFile(pPath As String, pPathToSave As String)
Dim pFileToPrepare As Workbook
Dim pSheet As Worksheet
Dim pLastRow As Long
Dim pName As String
Dim i As Long
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
Set pFileToPrepare = Workbooks.Open(pPath)
Set pSheet = pFileToPrepare.Worksheets(1)
pSheet.Rows("1:3").Select
Selection.Delete Shift:=xlUp
pSheet.Rows("2:5").Select
Selection.Delete Shift:=xlUp
pLastRow = pSheet.Cells(pSheet.Rows.Count, "A").End(xlUp).Row
For i = 2 To pLastRow
If pSheet.Cells(i, 1).Value = "" Then
pSheet.Rows(i & ":" & i + 5).Select
Selection.Delete Shift:=xlUp
End If
Next i
pName = pFileToPrepare.Name
pName = pPathToSave & pName
Debug.Print pName
pFileToPrepare.SaveAs pName
pSourceName = pFileToPrepare.Name
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub
Sub prcPrepareFirstReport(pTargetPath As String)
Dim pSourceWorbook As Workbook
Dim pTargetWorkbook As Workbook
Set pSourceWorbook = Workbooks(pSourceName)
Set pTargetWorkbook = Workbooks.Open(pTargetPath)
End Sub
我试过这样的版本
pExcel.Application.Run pMacro
,但它不起作用......
我试图只运行第二个宏,它就在......
请帮忙。
答案 0 :(得分:0)
看起来您的公共变量pSourceName
在调用代码中对Application.Run
的两次调用之间失去状态。比使用全局状态变量更强大的解决方案是将prcPrepareFile
转换为返回pSourceName
的函数,然后将 传递给prcPrepareFirstReport
。
Excel代码:
Public Function prcPrepareFile(pPath As String, pPathToSave As String) As String
'Snip
pName = pFileToPrepare.Name
pName = pPathToSave & pName
pFileToPrepare.SaveAs pName
prcPrepareFile = pFileToPrepare.Name
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Function
Sub prcPrepareFirstReport(pTargetPath As String, pSourceName As String)
Dim pSourceWorbook As Workbook
Dim pTargetWorkbook As Workbook
Set pSourceWorbook = Workbooks(pSourceName)
Set pTargetWorkbook = Workbooks.Open(pTargetPath)
End Sub
致电代码:
Dim pSourceName As String
pSourceName = pExcel.Run(pMacro, pPath, pPathToSave)
pMacro = "prcPrepareFirstReport"
pTargetPath = DLookup("[F_LINK]", "tblLinks", "[F_ID] = 5")
pExcel.Run pMacro, pTargetPath, pSourceName
答案 1 :(得分:0)
好的,我想通了。来自@Comintern和@Rory的帮助非常重要:)
所以问题是,在运行prcPrepareFile
之后,其他Excel被激活,而不是宏代码。我不知道为什么(可能有人可以解释),但在此之后变量pExcel
中有第二个没有VBA代码的Excel ......
解决方案是使用第一个Excel的名称准备变量,然后使用第一个Excel的名称运行第二个宏。正确的代码如下:
Dim pSourceName As String
Dim pWorkWorkbookName As String
pWorkWorkbookName = pExcel.Names.Parent.Name
pSourceName = pExcel.Run(pMacro, pPath, pPathToSave)
pMacro = "prcPrepareFirstReport"
pTargetPath = DLookup("[F_LINK]", "tblLinks", "[F_ID] = 5")
pExcel.Run "'" & pWorkWorkbookName & "'!" & pMacro, pTargetPath, pSourceName
谢谢大家的帮助。