本文介绍了Excel vba:未定义属性让程序和属性获取程序未返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Client 类.在那个类里面有一个数组losses.首先,我创建并用客户端填充一个 clientsColl 数组.然后对于该数组中的每个客户端,我填充它的 losses 数组.

I have a Client class. Inside that class there is an array losses. First I create and populate with clients a clientsColl array. Then for each client in that array I populate its losses array.

然后我尝试为每个客户端打印调试 losses 的第一个元素.但是,它不起作用并且出现Property let procedure not defined 和property get procedure did not return an object 错误.

Then I try to print into debug a first element of losses for each client. However, it doesnt work and Property let procedure not defined and property get procedure did not return an object error appears.

同时,如果我只是尝试为 第一个客户端显示 losses 的第一个元素,没有任何循环,它工作正常:

And the same time if I just try to display a first element of losses for the first client, without any cycle, it works fine:

Dim clientsColl() As Client
clientsColl = getClients(dataWorkbook)

Dim clientCopy As Variant    

Debug.Print "first: " & clientsColl(1).getLosses(1) 'works fine
For Each clientCopy In clientsColl
    Debug.Print "in for each: " & clientCopy.getLosses(1) 'error here
Next

Client 类中:

Public Property Get getLosses()
    getLosses = losses
End Property

Private losses() As Double

如何填充 losses 数组:

Public Sub calculateFinancialResult()
    ReDim losses(1 To simulationCount)
    ReDim profits(1 To simulationCount)

    Dim i As Long
    For i = 1 To simulationCount
        If outcomes(i) = 1 Then
            losses(i) = totalLoss
            ...
        Else
            ...
        End If
    Next
 End Sub

为什么会发生这种情况以及如何解决?

Why does this happen and how to fix it?

更多的主要子:

For Each clientCopy In clientsColl
        clientCopy.setSimulationCount = globals("SIMULATION_COUNT")
        ...
        clientCopy.calculateFinancialResult
        ...
Next

同时一个简单的 for 循环工作正常:

At the same time a simple for cycle works fine:

Debug.Print "first: " & clientsColl(1).getLosses(1)
For tempCount = LBound(clientsColl) To UBound(clientsColl)
    Debug.Print "in for each: " & _
                clientsColl(tempCount).getLosses(1)
Next

推荐答案

你的 getLosses 属性不带参数,所以你的语法实际上是错误的,即使 VBA 可以在早期绑定时处理它.您应该使用:

Your getLosses property doesn't take an argument so your syntax is actually wrong, even though VBA can cope with it when early bound. You should be using:

Debug.Print "first: " & clientsColl(1).getLosses()(1) 'works fine
For Each clientCopy In clientsColl
    Debug.Print "in for each: " & clientCopy.getLosses()(1) 'error here
Next

这篇关于Excel vba:未定义属性让程序和属性获取程序未返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 19:51