Chr*_*ris 2 arrays excel vba excel-vba
有人可以解释为什么会这样吗?我有一个数组变量Loans(),它存储了几个我需要稍后用于格式化数据透视表的短字符串.值从"基金清单"表中存储.然后表单被删除,但我仍然需要使用这些值.当我在删除工作表后尝试使用Loans()变量时,Excel返回一个对象必需的错误.我发现这方面的方法是不删除工作表,但有谁知道这是为什么?相关代码如下.
Sheets("Fund List").Select
' some code
Dim Loans() As Variant
Dim index As Integer
If LN > 0 Then
ReDim Loans(LN - 1)
index = 0
For i = 1 To LastRow
If Range("H" & i).Value = 3 Then
Set Loans(index) = Range("A" & i)
index = index + 1
End If
Next i
End If
' some more code
Sheets("Fund List").Delete
' more code
Set objField = objTable.PivotFields("Fund")
If LN > 0 Then
For index = 0 To UBound(Loans)
' I get an object required error on the following line:
LNFund = Loans(index)
objField.PivotItems(LNFund).Visible = False
Next index
End If
Run Code Online (Sandbox Code Playgroud)
小智 6
您正在将变量数组元素设置为Range对象.
Set Loans(index) = Range("A" & i)
Run Code Online (Sandbox Code Playgroud)
改为分配Range.Value或Range.Value2属性.
Loans(index) = Range("A" & i).Value2
Run Code Online (Sandbox Code Playgroud)
注意不是Set.