Excel / VBA:不能得到Variant /布尔返回(应该是微不足道的)

我不能得到这个函数将我的值返回到Excel中的输出列:

为了克服一些激烈的查找表并加速计算,我使用切片器的数据透视表来输出来自过滤的行号。 这些行然后需要被转换成一个真正/假的单元格列的大表,从那里我然后要执行更多的计算。 为了避免查找或匹配,我只需要遍历行列表,并在输出向量中将这些单元格变为“真”。

Function IncludedinSlicer(input_range As Variant) As Variant Dim n As Long, j As Long, r As Long n = input_range.Height ' Height of the column of reference values ' every row in the input_range contains a row number which in the output should be TRUE ' all other rows should be false Dim output_range As Variant ReDim output_range(1 To 300000) ' This covers the maximum number of rows ' Initialise all rows to FALSE For j = 1 To 300000 output_range(j) = False Next j ' Set only those rows listed in the reference to TRUE For j = 1 To n r = input_range(j).Value If r = 0 Then ' If r=0 then we are beyond the end of the reference table and have captured some blank rows Exit For ' Exit, to avoid outside-of-array errors Else output_range(r) = True End If 'End If Next j ' Return results to Excel ' THIS LAST BIT DOES NOT RETURN VALUES TO EXCEL IncludedinSlicer = output_range End Function 

我知道这应该是微不足道的,但不知何故它使我几个小时困扰。 请帮忙! 先谢谢你!

编辑:发现问题!

首先,感谢您指出Height和Rows.Count之间的差异,因为我没有意识到这一点。

不幸的是,在最后的单元格输出(#Value)中仍然留下了相同的错误。 幸运的是,在此期间,我尝试通过Matlab来处理这个问题,当传回结果时,我得到了同样的错误。 这使我能够缩小问题的范围,并追踪到错误…(鼓卷)… VBA的arrays大小的2 ^ 16限制。 我的表格已经接近2 ^ 18行,所以这会导致错误。

input_range.Height指的是范围的像素的文字高度。 尝试改为input_range.Rows.Count来获取范围内的行数。

对于你的函数,你应该传递一个Rangetypes,而不是模棱两可的Varianttypes。

这将使您可以直接访问Rangetypes的所有属性(即行,列,单元格),并可能更容易地捕捉到bobajob指出的问题。