2016-11-08 22 views
0

我不能得到這個功能以我的價值觀返回輸出列在Excel:的Excel/VBA:無法獲取株/布爾返回(應該是微不足道的)

爲了克服一些激烈的查找表和加速計算,我正在使用帶切片器的數據透視表來輸出來自過濾的行號。然後需要將這些行轉換爲一個真正/假的單元格列,以便從中執行更多計算。爲了避免查找或匹配,我只需要遍歷行列表並在輸出向量中將這些單元格變爲「真」。

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的陣列大小的2^16限制。 我的表格已經接近2^18行,所以這會導致錯誤。

+0

你的代碼錯誤(如果是的話)或只是返回一個意外的結果? – SJR

回答

1

input_range.Height指的是以範圍的像素爲單位的文字高度。試試input_range.Rows.Count以獲取範圍內的行數。

0

對於你的函數,你應該傳入(並可能返回)一個Range類型而不是含糊的Variant類型。

這將使您可以直接訪問範圍類型(​​即行,列,單元格)的所有屬性,並可能更容易地捕捉到bobajob指出的問題。