我试图通过将数组设置为等于Excel范围( n Rows X 1 Column
)来分配2D数组。
当( n = 1
)时,这只是给我一个值,因此在这种情况下,我想手动分配数组。我尝试了以下操作,但其中填充了Error 2015
Function Get_Params(ws As Worksheet, LR As Long, Target As Range) As Variant
Dim Temp As Worksheet: Set Temp = ThisWorkbook.Sheets("Temp")
Dim LR2 As Long
ws.Range("D1:D" & LR).SpecialCells(xlCellTypeVisible).Copy
Temp.Range("U1").PasteSpecial xlPasteValues
Temp.Range("U1").RemoveDuplicates 1, xlYes
LR2 = Temp.Range("U" & Temp.Rows.Count).End(xlUp).Row
If LR2 = 2 Then
Get_Params = [{Temp.Range("U2"), 1}] '<-- ISSUE HERE
Else
Get_Params = Temp.Range("U2:U" & LR2).Value
End If
Temp.Range("U1").EntireColumn.ClearContents
End Function
目标是要做
For i = LBound(Get_Params, 1) to UBound(Get_Params, 1)
'Do something with Get_Params(i, 1)
Next i
我尝试使用Evaluate
函数通过this解决方案手动分配数组,但这会产生我的错误。我也尝试过使用Get_Params = [{Temp.Range("U2").Value, 1}]
并收到相同的错误
我如何手动分配该数组以使我能够顺利通过上述循环?
答案 0 :(得分:2)
您可以替换
Get_Params = [{Temp.Range("U2"), 1}]
通过
Dim returnValue As Variant
'... then in the if statement:
ReDim returnValue(1 to 1, 1 to 1)
returnValue(1,1) = Temp.Range("U2").Value
Get_Params = returnValue