修改公共数组的值不起作用
我有一个问题来修改一个声明为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
当我debugging我看到MyArray(0)例如永远不会更改为1
我总是使用公共variables,这是工作,但不是数组的
阅读数组是好的,但不能修改值…
你有什么想法?
谢谢,
这可以实现声明数组私人和创build像Get / Set MyArrayValue函数。 然后,在UserForm1创buildUserForm3之后,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