Rss & SiteMap

金字塔客服中心 - 专业程序化交易软件提供商 http://www.weistock.com/bbs/

专业程序化软件提供商
共9 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:容易造成系统崩溃的VBA,请版主帮忙调试。

1楼
pel46585 发表于:2014/12/24 9:15:52
可能我的思路上有问题,代码写的有些乱,并且因为时常出现错误,为了调试,加了On Error Resume Next,后果是容易崩溃。
我的目的是键入数字1,更换主图为MA,副图为KDJ,键入2时,更换为其它公式。 能用其它的方式解决更好。
Sub SelectFormula()
On Error Resume Next
If Application.IsActivateFrame("Technic") <> 1 Then
Msgbox "不是技术分析页面,无法加载公式组"
Exit Sub
End If
txtInput = inputbox("选择公式组。1、MA KDJ;2、SAR MACD")
If txtInput = "1" Then TxtSelect = "MA KDJ"
If txtInput = "2" Then TxtSelect = "SAR MACD"
TXT=Split(TxtSelect," ")
Set Grid = Technic.GetGridByName("Main")
Set Formula = Grid.GetFormulaByIndex(1)
If  Err.Number = 61704 Then
Grid.InsertFormula txt(0),3
Err.Number = 0
End If
If Err.Number = 0 Then
Grid.InsertFormula txt(0),3
Grid.DeleteFormula Formula.name
End If
Set Grid = Nothing
Set Formula = Nothing
Set Grid = Technic.GetGridByName("Window9")
'msgbox Grid.FormulaCount & Grid.Name
Set Formula = Grid.GetFormulaByIndex(0)
If  Err.Number = 61704 Then
Error.Clear
Set Formula = Grid.GetFormulaByIndex(1)
application.MsgOut Err.Number
Grid.InsertFormula txt(1),3
Err.Number = 0
End If
If Err.Number = 0 Then
Grid.InsertFormula txt(1),3
'Grid.DeleteFormula Formula.name
End If
End Sub
2楼
王锋 发表于:2014/12/24 13:31:21
你这是完整的可以直接运行的代码吗?
3楼
pel46585 发表于:2014/12/24 15:33:18
是的,但有错误或不太好的地方。去掉ON ERROR RESUME NEXT后,总是提示错误。不知道要怎么解决。
4楼
王锋 发表于:2014/12/24 19:12:17

直接用执行宏就可以跑了?

5楼
pel46585 发表于:2014/12/26 13:30:15
是的,反复多运行几次。
可能就会崩溃
6楼
pel46585 发表于:2014/12/31 14:41:40
暂时看,这个代码,如果运行一次vba后;重启金字塔,再重新运行代码是不出错的。但连续运行2次vba就出错。
考虑认为是:Grid.GetFormulaByIndex(i)部分出了错,错误原因,应当是没有正确释放Formul对象。
帮助文档中没有正确释放Formul对象或者正确释放Grid对象的代码。
我按一般的VB代码写的 Set Formula = Nothing,试验证明无效。

[此贴子已经被作者于2014/12/31 14:43:40编辑过]
7楼
pel46585 发表于:2014/12/31 14:50:01
运行次数多后,因为有ON ERROR RESUME NEXT,最终造成不可知原因,使金字塔崩溃且不可修复。
怎么解决?
8楼
guotx2010 发表于:2014/12/31 16:46:06

用3.24版试试看会不会崩溃?

9楼
王锋 发表于:2014/12/31 20:59:58

导致系统出异常的原因是

 

Set Formula = Grid.GetFormulaByIndex(1)

 

你这句话,你要判断系统当前的Grid加载了那么多公式后再取,否则取到的Formula对象是空的

共9 条记录, 每页显示 10 条, 页签: [1]


Powered By Dvbbs Version 8.3.0
Processed in 0.03320 s, 2 queries.