修改公共数组的值不起作用

时间:2016-02-22 09:36:26

标签: arrays excel vba

我有一个问题是修改声明为public的数组的值。

所以有我的代码:

在UserForm1的声明中我有

Public MyArray as Variant

私有子UserForm_activate()"我有:

MyArray = Array(0, 0, 0, 0, 1)

直到它工作

我在UserForm3中的

Private Sub CheckBox1_Click()
If UserForm1.MyArray(4) = 1 Then
    UserForm1.MyArray(0) = 1
    UserForm1.MyArray(4) = 0
ElseIf UserForm1.MyArray(0) = 1 Then
    UserForm1.MyArray(0) = 0
    UserForm1.MyArray(4) = 1
End If
End Sub

当我调试时,我看到MyArray(0)例如永远不会改为1

我总是使用公共变量,这是有效但不是数组的

读取数组没问题,但没有修改数值......

你有什么想法吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

这可以在声明数组私有并创建Get / Set MyArrayValue等函数时实现。然后在UserForm1创建UserForm3之后,将设置Parent属性,以便UserForm3可以到达Get / Set MyArray的UserForm1函数。 HTH

  

UserForm1 - 父表单

Option Explicit

Private m_MyArray As Variant
Private m_childForm As UserForm3

Private Sub CommandButton1_Click()
    m_childForm.Show
End Sub

Private Sub UserForm_Initialize()
    Set m_childForm = New UserForm3
    Set m_childForm.MyParent = Me
    m_MyArray = Array(0, 0, 0, 0, 1)
End Sub

Public Function GetMyArrayValue(index) As Variant
    GetMyArrayValue = m_MyArray(index)
End Function

Public Sub SetMyArrayValue(index, newValue)
    m_MyArray(index) = newValue
End Sub
  

UserForm3 - 子表单

Option Explicit

Private m_parent As UserForm1

Private Sub CheckBox1_Click()
    If m_parent.GetMyArrayValue(4) = 1 Then
        m_parent.SetMyArrayValue 0, 1
        m_parent.SetMyArrayValue 4, 0
    End If
End Sub

Public Property Set MyParent(ByVal vNewValue As UserForm)
    Set m_parent = vNewValue
End Property