VBA动态数组:更改Get Let Property的大小和使用

时间:2016-10-18 04:21:23

标签: arrays excel vba dynamic properties

问题:我想创建一个动态数组,在事件发生时(事件计算)改变大小,保留其内容并扩展其大小。让我们说我想为双向(也是日期类型)的向量做这个。数据正在特定单元格中更新。

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

Classe的名字:" 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

所以我想"让"归因于扩展数组的新值 我想" Get"返回数组(扩展和更新) 备注:计数器将在Event" 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?正如你所看到的,有些观点使我对使用和结构感到困惑。

谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

编辑以符合以下假设

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

    我会在Sub ExploitClass1()之后调用它,但您可以根据需要重命名

  • Sub写入相关工作表的任何单元格,其计算事件要用于更新变量Class1类型

    的动态数组属性

    我假设相关工作表以" CalculateClass"命名:您可以根据需要重命名它,但请务必在其代码窗格中填写您在&#中找到的内容34;您的相关工作表代码窗格"这个答案的一部分

然后继续如下:

您的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

您的相关工作表代码窗格

它将使用我们在Public sub

中声明和初始化的Class1类型的ExploitClass1()对象
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