如何在程序里调用运行Excel里的宏?

darkmoon 2004-10-27 10:16:49
如统计分析函数里的宏,加载后用Excel可以直接调用,用录制能跟踪出来,但在程序里不能运行。
Application.Run "xxxx!yyyy",......
用Excel对象或者Ole都不行。
???
...全文
620 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2004-10-28
  • 打赏
  • 举报
回复
HOWTO: Call Microsoft Excel Macros That Take Parameters
The information in this article applies to:
Microsoft Excel 2000
Microsoft Visual Basic Learning Edition for Windows 5.0
Microsoft Visual Basic Learning Edition for Windows 6.0
Microsoft Visual Basic Professional Edition for Windows 5.0
Microsoft Visual Basic Professional Edition for Windows 6.0
Microsoft Visual Basic Enterprise Edition for Windows 5.0
Microsoft Visual Basic Enterprise Edition for Windows 6.0
Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
Microsoft Visual Basic Professional Edition, 16-bit, for Windows 4.0
Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows 4.0
Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
Microsoft Visual Basic for Applications 5.0
Microsoft Excel 2002
Microsoft Excel 97 for Windows
This article was previously published under Q153307
SUMMARY
Using Automation, you can manipulate Microsoft Excel. It is possible to call macro procedures that are stored in Microsoft Excel Workbooks by using the Run method of the Microsoft Excel Application object. Microsoft Excel macro procedures that take parameters cause a slight change in the syntax. Included below is a code sample showing how to call a Microsoft Excel macro procedure from Visual Basic.
MORE INFORMATION
Step-by-Step Example
Start a new Project in Visual Basic. Form1 is created by default.
Place a CommandButton on Form1.
In the General Declarations section of Form1, enter this code:
Option Explicit

Private Sub Command1_Click()
Dim oExcelApp As Object

' Create a reference to the currently running excel application
Set oExcelApp = GetObject(, "Excel.application")
' Make the Excel Application Visible.
oExcelApp.Visible = True
' Run the excel procedure
oExcelApp.run "proc", "David", 30
End Sub

Start Microsoft Excel. Book1 is created by default.
Add a new module to the workbook.

For Excel 5.0 and 7.0: From the Insert menu, choose Macro, and select the Module Option. This will give you a new module sheet, Module1.

For Excel 97 and later: Press ALT+F11 to start the Visual Basic Editor. Click Module on the Insert menu.
In Module1, type the following code:
Sub Proc(sParam1 As String, iParam2 As Integer)
MsgBox sParam1 & " is " & iParam2 & " Years Old"
End Sub

Leave the workbook open in Microsoft Excel and switch to your project in Visual Basic.
From Visual Basic, press F5 to run the project. Click the command button, and you should see a dialog box appear with the text "David is 30 years old" in it.
darkmoon 2004-10-28
  • 打赏
  • 举报
回复
可以试试在EXCEL菜单-->工具-->加载宏里加载数学统计分析函数。
里面的函数都可以用,但是用代码调用不了
darkmoon 2004-10-28
  • 打赏
  • 举报
回复
TO 1楼 2楼:不行,EXCEL提供的统计分析宏代码是加密的,看不了,只知道接口,录制的宏代码就是
Application.Run "XXXXX.xla!YYYY",,.....
但是我试了,用EXCEL.APPLICATION 对象或者用OLE对象都不能运行,能找到对应的xla文件,应该是封装的宏代码,在EXCEL打开后不能浏览的,需要密码。
darkmoon 2004-10-28
  • 打赏
  • 举报
回复
TO 楼上的,你说的方法也是调用一般的宏。
我想知道的是如何调用EXCEL里提供的一些内置的宏的方法。
像我说的数学统计分析函数,实际上就是一个XXXX.XLA文件。录制宏发现是通过"!方法"来实现的。

录制的宏代码就是如下:
Application.Run "XXXXX.xla!YYYY",,.....

但是该代码在程序里通过EXCEL对象是没有办法执行的。当然,如果写在自定义宏里,通过运行宏可以运行。但是存在很多参数的问题,有没有办法直接通过EXCEL对象,比如APPLICATION来直接运行该宏的方法,这样才容易在程序里控制,比如参数什么的。

如果解决,分不是问题。

jam021 2004-10-27
  • 打赏
  • 举报
回复
在VB里执行c:\22.doc中的test1宏,如下:

Dim wdApp
Dim doc
Set wdApp = CreateObject("word.application")
wdApp.Visible = True
Set doc = wdApp.Documents.Open("c:\22.doc")
doc.Application.Run "test1"
dlqhit 2004-10-27
  • 打赏
  • 举报
回复
将录制的代码直接拷到程序,在每行代码的range,selection前加上xlsheet,xlapp等
lxcc 2004-10-27
  • 打赏
  • 举报
回复
把宏代码直接放到VB内执行即可!

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧