我一直在梳理互联网以寻找答案而找不到任何东西。对于我的生活,我不能从函数返回一个字符串数组。我也尝试过变种,但我得到的只是语法错误。
Private Function set_device_list(ByVal deviceListSize As Integer) As Variant()
Dim loopIndex As Integer
loopIndex = 0
Dim firstRow As Integer
Dim lastRow As Integer
Dim firstColumn As Integer
Dim lastColumn As Integer
firstRow = 2
lastRow = 2
firstColumn = 4
lastColumn = 63
Dim deviceStartIndex As String
Dim deviceEndIndex As String
Dim deviceList() As Variant
ReDim deviceList(0 To (deviceListSize - 1))
deviceStartIndex = Cells(firstRow, firstColumn)
deviceEndIndex = Cells(lastRow, lastColumn)
For i = firstColumn To lastColumn
deviceList(loopIndex) = Cells(firstRow, i).Value
loopIndex = loopIndex + 1
Next i
Return deviceList
End Function
答案 0 :(得分:1)
只需使用set_device_list = deviceList
,这就是vba等同于大多数c语言的返回表达式。
要将其分配给字符串数组,请执行以下操作:
Dim myArray() As String
Dim device_list() as Variant
device_list = set_device_list(yourNumber)
For i = 1 To UBound(device_list)
myArray(i) = CStr(device_list(i))
Next
答案 1 :(得分:0)
请在函数声明的末尾删除括号: “......作为变体”不是“......作为变体()”
尝试代替“返回设备列表”输入“set_device_list = deviceList”
答案 2 :(得分:0)
实际上没有实际的"返回" VBA中的功能。 您可以做的是将函数的结果分配给变量。
例如:
Dim test As Integer
Dim dummy As Integer
Function do_something(parameter) As Integer
For i = parameter to 5
dummy = dummy + i
Next i
do_something = dummy
End Function
Sub your_sub
Dim test_var As Integer
test_var = do_something(0)
End Sub
我希望你能理解我想说的话