【问题标题】:VBA Dynamic array : change size and use of Get Let PropertyVBA 动态数组:更改大小和使用 Get Let 属性
【发布时间】:2017-02-27 05:00:23
【问题描述】:

问题:我想创建一个动态数组,当事件发生时改变大小(事件计算),通过保留其内容并将其大小扩展一。假设我想为双精度向量(以及日期类型)执行此操作。特定单元格中的数据正在更新。

我的理解:我正在“计算 Excel”事件中编码。我不能使用“公共”,我必须将数组声明为私有,然后使用 Get 和 Let Property...但是,在这种情况下如何使用 Redim Preserve?另外,我想我可能遗漏了一些关于如何使用它的要点:这是我编写的代码示例:

类名称:“Class1” 班级代码:

代码:

Option Explicit
Private IntradayValueSerie1() As Double 'Dynamic vector which will contain Y1 Values
Public Counter As Long
Public Property Let vIntradayValueSerie1(ByVal Counter_Value As Long)
      IntradayValueSerie1(Counter_Value) = Sheets("Sheet1").Range("C5")
End Property
Public Property Get vIntradayValueSerie1(ByVal Counter_Value As Long) As Variant
      vIntradayValueSerie1(Counterr) = IntradayValueSerie1
End Property

所以我想要“让”来赋予我扩展数组的新值 我希望“获取”返回数组(扩展和更新) 备注:计数器将在事件“Worksheet.Calculate”部分的末尾更新并增加


测试代码(必须在事件“Worksheet.Calculate”部分中)

代码:

counter = 1

Dim Serie1 As New Class1
Serie1.IntradayValueSerie1(Counter) = ??? I don't know how to use the property to initialize the vector

counter = counter +1
 ReDim Preserve IntradayValueSerie1(Counter)

另外,由于我想返回一个数组,我必须为 Get 属性设置 Variant 吗?如您所见,有些地方让我感到困惑,无论是在使用上还是在结构上。

感谢您的宝贵时间!

【问题讨论】:

    标签: arrays excel vba dynamic properties


    【解决方案1】:

    编辑以匹配以下假设

    • 任何模块中的Sub 初始化并使用Class1 类型的变量

      我会在Sub ExploitClass1()之后调用它,但是你可以随意重命名它

    • Sub 写入相关工作表的任何单元格,您希望使用其计算事件来更新 Class1 类型变量的动态数组属性

      我假设相关工作表以“CalculateClass”命名:您可以随意重命名它,但请务必使用您在此答案的“您的相关工作表代码窗格”部分中找到的内容填充其代码窗格

    然后进行如下操作:


    您的 Class1 代码窗格

    Option Explicit
    
    Private IntradayValueSerie1() As Double 'Dynamic array which will contain Y1 Values
    Private counter As Long '<-- counter to track the size of the dynamic array
    
    Public Sub WriteValue(ByVal Value As Variant) '<-- class method to write a value in the last dynamic array slot
        IntradayValueSerie1(counter) = Value
        Extend
    End Sub
    
    Private Sub Extend() '<-- class method to extend dynamic array size by one
        counter = counter + 1 '<-- update the dynamic array size counter by one
        ReDim Preserve IntradayValueSerie1(1 To counter) '<-- increase the dynamic array size
    End Sub
    
    Private Sub Class_Initialize()
        counter = 1
        ReDim IntradayValueSerie1(1 To counter) '<-- at class instantiating, initialize the dynamic array
    End Sub
    
    '-----------------------------------------------
    ' added methods to "query" some dynamic array related values
    '-----------------------------------------------
    Public Function GetCounter() As Long '<-- class method to retrive the current counter (i.e. the dynamic array size) value
        GetCounter = counter '<-- return counter
    End Function
    
    Public Function GetPenultimateArrayValue() As Variant '<-- class method to retrive the current counter (i.e. the dynamic array size) value
        GetPenultimateArrayValue = IntradayValueSerie1(counter - 1) '<-- return dynamic array one before second to last element
    End Function
    

    你的Sub

    Option Explicit
    
    Public Serie1 As Class1 '<-- declare a Public variable of type Class1
    
    Sub ExploitClass1()
        Set Serie1 = New Class1 '<-- instantiate a new public object of type Class1
        
        Worksheets("CalculateClass").Range("A1") = 1 ' make something that triggers calulate event in the relevant worksheet: in this case I had cell "A2" of that worksheet with a formula `= A1+1`
        
        MsgBox Serie1.GetPenultimateArrayValue & " - " & Serie1.GetCounter 'show your Class1 dynamic array has been updated exploiting those "querying" methods we added at the bottom of your class
    End Sub
    

    您的相关工作表代码窗格

    它将使用我们在ExploitClass1()子中声明和初始化的Class1类型的Public对象

    Option Explicit
    
    Private Sub Worksheet_Calculate()
        Serie1.WriteValue Worksheets("Sheet01").Range("C5").Value '<-- this will write the passed value to your class dynamic array last slot
    End Sub
    

    【讨论】:

    • @LaGabriella,你通过了吗?
    • @LaGabriella,如果你能对试图帮助你的人提供适当的反馈,那就太好了。谢谢
    猜你喜欢
    • 2014-08-03
    • 2014-06-19
    • 1970-01-01
    • 2014-03-31
    • 2019-08-11
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多