我不是一位经验丰富的VBA程序员,但我一直在努力创建一个能够管理篮球队的Excel电子表格。
在其中我已经有一个主要的用户形式,我已经声明了一个数组,' selectedPlayers'。 此主要用户表单具有for循环,用于启动辅助用户表单' i'倍。
我无法访问主要用户表单' i'和' selectedPlayers'来自中学。 我已经能够找到一个解决方法' i'通过在第一个用户表单中创建一个不可见的文本框,我可以从第二个用户表单中引用。
我已经尝试将这两个声明为公开,但我无法从第二个用户表单中调用它。
第一个用户表单的部分代码:
i = 0
Do While Not i = Int(txtNumberPlayers)
frmGameDataSecondary.Show
i = i + 1
Loop
第二个用户形式:
Private Sub cmdDone_Click()
frmGameData.selectedPlayers(frmGameData.i) = lbxPlayer.Value
Unload Me
End Sub
Private Sub UserForm_Initialize()
With Me.lbxPlayer
For Each LR In LO.ListRows
exitSequence = False
For k = 1 To Int(frmGameData.txtNumberPlayers)
If frmGameData.selectedPlayers(k) = blablabla.Value Then
exitSequence = True
End If
Next k
If !exitSequence Then
.AddItem blablabla.Value
End If
Next LR
End With
End Sub
答案 0 :(得分:1)
主要问题是子结束后数组内容被清除。
我也在喋喋不休地谈论这个想法,并且有一个非常好的主题,我开始收集来自各种令人敬畏的人的大量信息
答案 1 :(得分:0)
VBA中的表单是对象,可以像任何其他类模块一样对待。这意味着您可以向它们添加属性。如果您需要从表单中传回信息,您需要做的只是获取对它的引用,然后Hide
而不是Unload
它。像对话框一样对待它,让调用代码处理它的创建和销毁(我假设你的代码是模态的)。
这样的事情:
在第一个UserForm中:
For i = 0 To 1
Dim second As frmGameDataSecondary
Set second = New frmGameDataSecondary
second.Show
'Execution suspends until the second form is dismissed.
selectedPlayers(i) = second.Player
Unload second
Next i
在第二个UserForm中:
Private mPlayer As String
'This is where your returned information goes.
Public Property Get Player() As String
Player = mPlayer
End Property
Private Sub cmdDone_Click()
mPlayer = lbxPlayer.Value
'Control passes back to the caller, but the object still exists.
Me.Hide
End Sub
答案 2 :(得分:0)
您可以在parent form
内声明属性,这些属性将从外部操纵数组。 child form
需要引用parent
,以便可以调用此属性。 HTH
父表格
Option Explicit
' I have not been able to access the primary userform's
' 'i' and 'selectedPlayers' from the secondary one
Private selectedPlayers As Variant
Public Function GetMyArrayValue(index) As Variant
GetMyArrayValue = selectedPlayers(index)
End Function
Public Sub SetMyArrayValue(index, newValue)
selectedPlayers(index) = newValue
End Sub
Private Sub UserForm_Click()
Dim i
i = 0
Do While Not i = Int(txtNumberPlayers)
With New secondaryUserForm
Set .ParentForm = Me
.SetIndex = i
.Show
End With
i = i + 1
Loop
End Sub
Private Sub UserForm_Initialize()
selectedPlayers = Array("A", "B", "C")
End Sub
儿童表格
Option Explicit
Private m_parent As primaryUserForm
Private m_index As Integer
Public Property Let SetIndex(ByVal vNewValue As Integer)
m_index = vNewValue
End Property
Public Property Set ParentForm(ByVal vNewValue As UserForm)
Set m_parent = vNewValue
End Property
Private Sub cmdDone_Click()
' frmGameData.selectedPlayers(frmGameData.i) = lbxPlayer.Value
m_parent.SetMyArrayValue m_index, "lbxPlayer.Value"
Unload Me
End Sub