我有以下代码:
Option Explicit
Public Function LastWeekOfMonth() As Boolean
'finds the current date
Dim CurrentDate As Date
CurrentDate = CDate(ActiveSheet.Cells(FIRST_DATA_ROW, 1))
'find filepath and filename
Dim filepath As String
Dim filename As String
filepath = "C:\target\file\path\here\"
filename = Cells(3, 4) & ".m_d.xlsm"
MsgBox (filepath & filename)
'if it is the last week of the month, write a monthly report, and return true to continue with the face to face paperwork
If (31 - Day(CurrentDate)) <= 7 Then
'write a monthly report
Dim app As New Excel.Application
Dim wsheet As New Worksheet
Dim book As Excel.Workbook
app.Visible = False 'Visible is False by default, so this isn't necessary
Set book = app.Workbooks.Open(filepath & filename)
Set wsheet = book.Worksheets("Monthly")
'Application.Run ("WriteMonthly")
app.book.wsheet.Run ("WriteMonthly")
book.Close SaveChanges:=True
app.Quit
Set app = Nothing
LastWeekOfMonth = True
'if it is not, simply continue with the face to face paperwork
Else
LastWeekOfMonth = False
End If
End Function
WriteMonthly是目前包含在目标电子表格中的宏。在线文档说可以通过application.run运行宏(宏名称,arg1,arg2,arg3,...)
我想从当前电子表格中提取信息以调用特定的电子表格并在该电子表格上运行宏,该宏的代码位于该电子表格上,最终生成的所有数据都存储在该电子表格中。
这是查看是否已达到该月最后几天的表单的一部分;如果他们有,写一个特定的&#34;每月&#34;报告。
问题在于:
app.book.wsheet.Run ("WriteMonthly")
不起作用,也不起作用:
Application.run("!WriteMonthly")
答案 0 :(得分:1)
语法为Application.Run "'WbkName'!ProcedureName"
但是,由于您调用的过程驻留在对象模块(在本例中为工作表)中,语法也必须包含对象的名称,例如:Application.Run "'WbkName'!ObjectName.ProcedureName"
Application.Run "'" & filename & "'!ObjectName.WriteMonthly"
将ObjectName
替换为工作表VbModule
的{{1}}的名称。
尝试:Monthly
同时确保“月度”模块中的Application.Run "'" & filename & "'!" & wsheet.CodeName & ".WriteMonthly"
未声明为WriteMonthly