【发布时间】:2019-06-05 04:44:36
【问题描述】:
我想使用 Sub 读取 Excel 工作簿中列的值(从 cell(3, 1) 开始),然后将数组传递给 main 函数,但在 sub 中获得的值没有返回到主函数中的数组。
我目前在 Cells(3, 1) 和 (4, 1) 中有值,我知道 Sub 可以工作,因为我在 Sub 中放入了一个消息框,它会读取这两个值。
我尝试将 Sub 变成一个函数,将 Sub 参数的名称更改为与主函数 (tr_des) 中的数组相同的名称,以及很多类似的东西。
Option Explicit
Private Sub cmd_openform_Click() '"Main" function
Dim tr_des() As String
Call getDescriptions(tr_des)
uf_TestSelector.Show vbModal 'shows properly
MsgBox tr_des(1) 'shows empty MsgBox
End Sub
Sub getDescriptions(ByRef des_array() As String)
Dim descrip As String, size As Integer
Dim i As Integer
i = 0
size = 1
ReDim des_array(size)
Do While Cells(i + 3, 1).Value <> ""
des_array(i) = Cells(i + 3, 1).Value
MsgBox des_array(i) 'opens MsgBox with correct value both times
size = size + 1
ReDim des_array(size)
i = i + 1
Loop
End Sub
我希望 MsgBox tr_des(1) 从 excel 工作表的列中返回一个值,但它总是返回一个空的 MsgBox
【问题讨论】: