我是VBA的新手。在Excel中按顏色計算唯一單元格值VBA
Endstate - 在一個範圍內搜索並計數用戶指定的填充顏色計數合併單元格(我知道,合併遺址所有內容)的唯一單元格值爲一個整體單元格。
我已經編譯了下面的代碼,但它不能正常工作,任何幫助將不勝感激!
Function CountUniqueColorBlocks(SearchRange As Range, ColorRange As Range) As Long
Dim cell As Range, blocks As Range
Dim dict As Scripting.Dictionary
Set dict = New Scripting.Dictionary
Set blocks = SearchRange(1).MergeArea(1) ' prime union method (which requires >1 value)
For Each cell In SearchRange
If cell.Interior.Color = ColorRange.Interior.Color And Not dict.Exists(cell.Value) Then
dict.Add cell.Value, 0
End If
Next
CountUniqueColorBlocks = dict.Count
End Function
什麼是你期望得到什麼,你實際上得到? – QHarr
當我在Excel中運行代碼時,我得到的答案是實際計數的+1,我不確定原因。另外,我想知道除了使用腳本字典以外,是否還有一種更有效的方式來執行唯一值搜索,但沒有將函數限制爲只有數值。 –
+1是由於合併的單元格被視爲空白,所以空白是一個新的獨特值,並給您一個額外的。添加一個if語句來檢查Len(cell.value)> 0'以確保您忽略空格。 – tigeravatar