Arrays 在VBA Excel的不同子系统中引用同一数组

Arrays 在VBA Excel的不同子系统中引用同一数组,arrays,vba,excel,Arrays,Vba,Excel,我有一个函数,根据所选的OptionButton,用单元格值填充特定数组。我如何在一个单独的函数中引用这些相同的数组,该函数将这些值反馈到单元格中?这是到目前为止我的(工作)代码 Private Sub CommandButton1_Click() Dim wave1Array(0 To 30) As String Dim wave2Array(0 To 30) As String Dim wave3Array(0 To 30) As String Dim wave4Array(0 To 30)

我有一个函数,根据所选的OptionButton,用单元格值填充特定数组。我如何在一个单独的函数中引用这些相同的数组,该函数将这些值反馈到单元格中?这是到目前为止我的(工作)代码

Private Sub CommandButton1_Click()
Dim wave1Array(0 To 30) As String
Dim wave2Array(0 To 30) As String
Dim wave3Array(0 To 30) As String
Dim wave4Array(0 To 30) As String
Dim wave5Array(0 To 30) As String
Dim rng As Range
Dim cell As Range
Dim counter As Long

Set rng = Range("B2", "AF2")
counter = 0

If OptionButton6.Value = True Then
    For Each cell In rng
    wave1Array(counter) = cell.Value
    counter = counter + 1
Next cell
ElseIf OptionButton7.Value = True Then
    For Each cell In rng
    wave2Array(counter) = cell.Value
    counter = counter + 1
Next cell
ElseIf OptionButton8.Value = True Then
    For Each cell In rng
    wave3Array(counter) = cell.Value
    counter = counter + 1
Next cell
ElseIf OptionButton9.Value = True Then
    For Each cell In rng
    wave4Array(counter) = cell.Value
    counter = counter + 1
Next cell
ElseIf OptionButton10.Value = True Then
    For Each cell In rng
    wave5Array(counter) = cell.Value
    counter = counter + 1
Next cell
End If

End Sub

你有几个不同的选择,我可以考虑

正如其他人所提到的,根据需要制作一个模块级变量。这些声明应该与表单控件放在同一个代码模块中。如果表单控件位于userform上,那么它们应该在表单的代码模块中声明,而不是在“标准”模块中声明

Public/GLobal变量可能是一个选项,但我记得在UserForms中使用这些变量有一些限制,因为我不确定您是否使用UserForm,所以我不建议这样做

第三种选择是将参数从一个过程传递到另一个过程,但这通常只适用于“链接”过程/函数,例如当一个函数调用另一个函数时,而这似乎根本不是您正在做的事情

针对您的具体情况:

您还可以简化代码,避免使用
计数器
单元格
变量,使用直接范围到数组赋值

'Module-level array variables, accessible by other procedures in this module:
Dim wave1Array()
Dim wave2Array()
Dim wave3Array()
Dim wave4Array()
Dim wave5Array()
Dim wave6Array()

Private Sub CommandButton1_Click()

Dim rng As Range
Dim arr() 

Set rng = Range("B2", "AF2")
'## Converts the row to an array (0 to 30)
arr = Application.Transpose(Application.Transpose(rng.Value))

'## Assigns the array from above to one of the module-level array variables:

If OptionButton6.Value = True Then wave1Array = arr
If OptionButton7.Value = True Then wave2Array = arr
If OptionButton8.Value = True Then wave3Array = arr
If OptionButton9.Value = True Then wave4Array = arr
If OptionButton10.Value = True Then wave5Array = arr
If OptionButton11.Value = True Then wave6Array = arr

End Sub
注意要执行此操作,您必须将它们声明为变量数组,因为一系列单元格
.Value
是一种变量类型(单元格可能包含错误值,我认为如果您尝试分配给字符串数组,这些错误值将失败)


如果你必须使用严格的字符串数组,那么你需要使用
计数器
单元格
迭代。

我不知道你到底想做什么,但是如果你把所有数组声明(
Dim wave1Array(0到30)作为字符串
,等等)从你的
子()
并将它们放在模块中,则可以从该模块中的任何子模块访问它们。。。不过,这也可能是一件坏事,因为您应该检查以确保它们在不同的时间具有您期望的值。@JohnBustos我是只接受数组声明,还是也可以移动计数器、范围和单元格声明?另外,我实际上如何在模块中声明数组?我把它们放在模块1中,但现在我的代码不起作用了。它找不到数组,只能取出数组声明本身(好的编程实践是尽量少取出),因为它们是其他SUB唯一需要的东西。并将它们放在该子模块当前所在的同一模块的最顶部。。。然后,该模块中的任何其他子模块也将能够引用这些(填充的)数组……您还可以使用全局而不是Dim声明它们,以便可以在整个程序中访问它们(在任何模块中的子模块之外)
'Module-level array variables, accessible by other procedures in this module:
Dim wave1Array()
Dim wave2Array()
Dim wave3Array()
Dim wave4Array()
Dim wave5Array()
Dim wave6Array()

Private Sub CommandButton1_Click()

Dim rng As Range
Dim arr() 

Set rng = Range("B2", "AF2")
'## Converts the row to an array (0 to 30)
arr = Application.Transpose(Application.Transpose(rng.Value))

'## Assigns the array from above to one of the module-level array variables:

If OptionButton6.Value = True Then wave1Array = arr
If OptionButton7.Value = True Then wave2Array = arr
If OptionButton8.Value = True Then wave3Array = arr
If OptionButton9.Value = True Then wave4Array = arr
If OptionButton10.Value = True Then wave5Array = arr
If OptionButton11.Value = True Then wave6Array = arr

End Sub