请看一下vba代码,每次执行生成一次excel进程

每次执行生成一个新的excel进程,怎么能执行完就关掉excel进程呢?
Sub Setdata(getColmns As Integer, setColmns As Integer, setZhangdie As Single)
Dim url As String
Dim mystr As String
Dim iLineBegin As Integer
Dim iCurLine As Integer
Dim I As Long
Dim j As Integer
Dim ss As Integer
Dim app, wb
Dim myarray
Const maxItemCount = 32
Dim maxcount

url = "http://hq.sinajs.cn/list="

iLineBegin = Me.Cells(1, 2) '取得开始行数
iCurLine = iLineBegin

' 获取数据
Set app = CreateObject("Excel.Application")
Set wb = app.Workbooks.Open(url)

iCurLine = iLineBegin
' 将获取的数据写入Excel
For I = 1 To 65535
mystr = wb.Sheets.Item(1).Cells(I, 1).Text '单个取
ss = InStr(mystr, ",")
If ss < 1 Then
Exit For
End If

If myarray(3) <> 0 Then
Me.Cells(iCurLine, setColmns).Value = myarray(3) '只要myarray(3)当前价即可,当前的
Else
Me.Cells(iCurLine, setColmns).Value = myarray(2) '只要myarray(3)如果当前价为0,则还未成交,取昨日收盘价
End If
If setZhangdie <> 0 And myarray(3) <> 0 Then Me.Cells(iCurLine, setZhangdie).Value = (myarray(3) - myarray(2)) / myarray(2) '算涨跌幅度 myarray(2)昨日收盘价

iCurLine = iLineBegin + I
Next I

wb.Close (False)
Set wb = Nothing
Set app = Nothing

End Sub
补充一下,这是在Excel2007中的vba代码

这个  是一个系统垃圾回收问题。当你按照qiaoxiaomao的设置了以后,有可能还有excel的进程,存在于系统中,这个可以从任务管理器中看见。等你这个程序运行完之后,你别管它,过一段时间,这个excel进程就消失了,被windows回收了。


如果要在你运行完程序,立即回收excel进程,可以参考以下方法:(适用于VB.NET,VBA的自己去网上查一查,原理一样)


private sub main()
call step1           'step1里面有你要调用的excel程序
gc.collect           '这句话可以让进程里面的那个excel立即消失,但是必须放在step1的外面,放在step1里面是没用的
end sub

private sub step1()
dim  ExlcelApp as new excel.aplication           '调用你的excel程序
dim myworkbook as  excelapp.workbooks
myworkbook=............

........
myworkbook.close
excelapp=nothing
end sub


注意那个GC.COLLECT的位置。。



纯手工打。。

追问

百度查了一下,在vba中似乎没有主动回收的机制。

追答

是么?反正你这个是写在excel里面的,那就不要那个application
把这两行改了
' 获取数据
Set app = CreateObject("Excel.Application")
Set wb = app.Workbooks.Open(url)

直接改为set wb=workbooks.open(url)

或者 把第一行里CreateObject("Excel.Application") 改为getobject(,"excel.application")

这样就只有一个进程了。

刚刚看了下,你的这个居然是一个股票程序。我看过别人高过这么个玩意,不过有的人直接用的是软件的数据,导出来,然后直接提取,用不着计算,涨跌幅啥的都有。

追问

是个自动从取股票数据的程序,没有计算,因为只取部分数据,所以有些处理过程。

你的第一个方法好用,解决了我的问题。第二个不行,报自动化错误

谢谢哈。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-20
app.Quit
Set app = Nothing

先退出,再设为空。追问

试过了,不行。

追答

app.Application.Quit 呢?

追问

试了,也不行。

相似回答