在For循环中使用“set”运行时错误'91'

时间:2017-10-15 08:50:17

标签: excel vba excel-vba com

Dim Srv As Object                                       
Dim Mdl As Object 

Dim SubVarGroups As Object
Dim SubVarGroup As Object

Dim nSubVarGroup As Integer                            
Dim nSubVar As Integer  

Dim FileName As String  

Set Srv = CreateObject("Simpack.Gui.90900") 
Set Mdl = Srv.Spck.openModel(FileName)  

Set SubVarGroups = Mdl.getSubvarGroupList(False)
nSubVarGroup = SubVarGroups.Count

Dim c As Integer, i As Integer, j As Integer

For i = 0 To nSubVarGroup

Set SubVarGroup = SubVarGroups.Item(i)
Worksheets("Test").Cells(i + 1, 1).Value = SubVarGroup.FullName

Next i

我使用此代码通过COM接口从第三方程序中获取一些信息。但是我得到了以下部分的“运行时错误”

Worksheets("Test").Cells(i + 1, 1).Value = SubVarGroup.FullName

运行时错误'91': 对象变量或未设置块变量

你能在我错的地方帮助我吗?

1 个答案:

答案 0 :(得分:0)

For i = 0 To nSubVarGroup

需要

For i = 0 To (nSubVarGroup-1)

因为只有nSubVarGroup项要循环,输入列表是从0开始的。   我无法在没有运行代码的情况下证明这一点,但我认为发生的事情是,在最后一次循环运行中,SubVarGroup被分配为空或null("未设置"),因此SubVarGroup.FullName阻塞。