我正在像这样运行一些嵌套循环
Worksheet Loop
Apply AutoFilter to all unique values in COLUMN C
Apply AutoFilter to all unique values in COLUMN D
'Do some stuff
我的目标是通过复制列的可见单元格,删除重复项并将剩余的行分配给数组,从Column A
和Column B
创建一个由唯一值组成的二维数组。然后,我可以简单地遍历每个数组,根据需要进行过滤,并对可见的单元格进行一些处理。
由于某种原因,第二次尝试分配Column B
数组时,在尝试分配temp数组时,在此行上收到类型不匹配错误({{1} }。发生这种情况时,请注意Get_Params
的值。
这是主循环,错误发生在标记行上的函数LR2 = 2
Get_Params
For Each ws In Worksheets
UnFilter ws
If ws.Name <> "Temp" Then
LR = ws.Range("C" & ws.Rows.Count).End(xlUp).Row
Set Target = ws.Range("A1:P" & LR)
Batches = Get_Batches(ws, LR)
For b = LBound(Batches, 1) To UBound(Batches, 1)
UnFilter ws
Target.AutoFilter 3, Batches(b, 1)
Params = Get_Params(ws, LR, Target)
For p = LBound(Params, 1) To UBound(Params, 1)
Target.AutoFilter 4, Params(p, 1)
'MsgBox TenPer(Target)
Next p
Next b
LR = 0
Set Target = Nothing
UnFilter ws
End If
Next ws
答案 0 :(得分:1)
您的函数被声明为数组,请参见As Variant()
Function Get_Params(ws As Worksheet, LR As Long, Target As Range) As Variant()
但是Range("U2:U2").Value
不会返回数组,而只是返回一个正常值,因为它只是一个单元格。因此,您会收到类型不匹配错误。
仅声明函数As Variant
:
Function Get_Params(ws As Worksheet, LR As Long, Target As Range) As Variant
然后,对于变量Params
,您可以检查If VarType(params) = vbArray Then
来捕获数组和非数组返回,并根据需要进行处理。