我的任务: 我有一个带有各种复选框的用户窗体。用户应选中/取消选中这些框,并在完成后按“完成”按钮。按下“完成”应该启动一个数组,并根据代码用字符串值填充它。到目前为止,一切都很好。
我的问题: 但是,因为我需要在另一个模块中使用该数组,所以它必须是公共的。我已将其声明为公共代码,如下面的代码所示。但是,它会出错。我在做什么错了?
谢谢!
Option Base 1
Public dimArray(5, 4) As String
Private Sub cmdBtn_Done_Click()
Unload ProductDimension
'1st Dimension
If chk_AbilityOfInteraction = True Then
dimArray(1, 1) = "Functionality"
dimArray(1, 2) = "Ability of interaction"
End If
If chk_Performance = True Then
dimArray(1, 1) = "Functionality"
dimArray(1, 3) = "Performance"
End If
....
End Sub
答案 0 :(得分:1)
由于要在另一个模块中获取用户表单的结果,因此可以在标准代码模块中使用以下代码。这是一个返回所需数组的函数。因此,在此过程中,您需要的数组应该是
Dim Arr() As String
Arr = GetArray
这是函数GetArray:-
Function GetArray() As String()
' "UserForm1" must be the name you gave to the UserForm.
' It's "UserForm1" by default but you did well if you changed the name.
Dim MyForm As UserForm1
Dim Arr(1 To 3) As String
Set MyForm = New UserForm1 ' this is the actual form's name, too
With MyForm
' although the form isn't visible, you can
' set or modify any of its controls:-
.chk_AbilityOfInteraction.Value = False
.Show ' now MyForm takes over control
' until "Hide" is encountered in
' the form's own code.
' Your "Done" button should trigger
' this action.
' upon "Me.Hide" the code resumes here
' The form is only hidden. All it's controls are still in memory.
Arr(1) = "Functionality"
If .chk_AbilityOfInteraction.Value = True Then
Arr(2) = "Ability of Interaction"
Else
Arr(3) = "Performance"
End If
End With
Unload MyForm ' now the form is killed
' if not killed you might "Show" it again
' with all previous values still in it.
Set MyForm = Nothing
GetArray = Arr ' this sets the function's return value
End Function