0
我似乎無法弄清楚這一點。我有一個函數和一個子函數,我調用函數來從我已經從自動過濾器中選擇的範圍中獲取唯一值(來自列N(文本值))。不知何故,範圍保持整個範圍,而不是選定的範圍。excel vba - 使用自動過濾器 - 無法將已過濾的範圍傳遞給子工具,它始終傳遞整個工作表範圍
Function UniquesFromRange(rng As Range)
Dim d As Object, c As Range, tmp
Set d = CreateObject("scripting.dictionary")
For Each c In rng.Cells
tmp = Trim(c.Value)
If Len(tmp) > 0 Then
If Not d.Exists(tmp) Then d.Add tmp, 1
End If
Next c
UniquesFromRange = d.Keys
End Function
Sub mainSub()
For Each key In fCatId.Keys
With wshcore
llastrow = wshcore.Range("A" & Rows.Count).End(xlUp).Row
.AutoFilterMode = False
.Range("A1:N" & llastrow).AutoFilter
.Range("A1:N" & llastrow).AutoFilter Field:=1, Criteria1:=fCatId(key)
lwmin = WorksheetFunction.Subtotal(5, Range("H:H"))
lwmax = WorksheetFunction.Subtotal(4, Range("H:H"))
'This does not work, I want to get the unique values from column N
'that are already in the filtered range. So far this shows
'all the values in the column not only the ones already filtered.
varArray = UniquesFromRange(Range("N:N"))
'I've also tried this:
'varArray = UniquesFromRange(Range.Cells)
'Debug.Print fCatId(key) & " - " & key & " " & lwmin & "-" & lwmax & fData(key) & " - " & Join(varArray, vbNewLine)
End With
Next key
Application.ScreenUpdating = True
End Sub
有什麼建議嗎?
謝謝!有用!需要等待幾分鐘,因爲該網站不讓我把它標記爲答案這麼快!我可以問你,我怎樣才能把這些值寫在新的紙上? – sys73r
@ sys73r - 我更新了答案,因爲簡單地通過'Range(「N:N」)'也會傳遞表格下方的所有可見單元格,這將減慢唯一值的查找速度(因爲您將有數千個空細胞看) – YowE3K
如何將Debug.print值複製到另一個新工作表? – sys73r