2017-02-15 61 views
1

這可能與scripting.dictionary從範圍中排除空白嗎? 我正在使用此代碼從範圍中查找特殊值。我不需要excel公式範圍,但在VBA解決方案(如果存在)。VBA - scripting.dictionary exlude blank

有了這個代碼,我在列表框總是一個空白項。

Dim v, e 

With Sheets("DATA").Range("NAMED_RANGE") 
    v = .Value 
End With 

With CreateObject("scripting.dictionary") 
    .CompareMode = 1 
    For Each e In v 
     If Not .Exists(e) Then .Add e, Nothing 
    Next 
    If .Count Then Me.TextBox121.List = Application.Transpose(.keys) 

    ThisWorkbook.Worksheets("DICTIONARY").Range("B2").Resize(UBound(.keys), 1).Value = _ 
    Application.Transpose(.keys) 
End With 
+2

添加如果e.Value <> 「」 然後檢查如果不.Exists(E)......之前 –

+1

'如果不.Exists(E)和E <> vbNullString然後。新增E, Nothing'作爲@ShaiRado所述 – R3uK

+0

@ R3uK這從你代碼工作,但如果範圍只有一個值有錯誤(「應用程序定義或deffined對象錯誤」) – Nataniell

回答

1
Dim v, e 

With Sheets("DATA").Range("NAMED_RANGE") 
    If Application.WorksheetFunction.CountA(.Cells) > 1 Then 
     v = .Value 
    Else 
     v = .Cells(1, 1).Value 
    End If 
End With 

With CreateObject("scripting.dictionary") 
    .CompareMode = 1 
    For Each e In v 
     If Not .Exists(e) And e <> vbNullString Then .Add e, Nothing 
    Next 
    If .Count Then Me.TextBox121.List = Application.Transpose(.Keys) 

    If .Count And UBound(.Keys)>0 Then ThisWorkbook.Worksheets("DICTIONARY").Range("B2").Resize(UBound(.Keys), 1).Value = _ 
     Application.Transpose(.Keys) 
End With 
+0

嗯仍然是一樣的錯誤。我在範圍內的數據是這樣的: 空,空 , TEST, 空,空 , TEST, – Nataniell

+0

@Nataniell:哪些錯誤,並在其上線? – R3uK

+0

This line ThisWorkbook.Worksheets(「DICTIONARY」)。Range(「B2」)。Resize(UBound(.Keys),1).Value = _ Application.Transpose(.Keys)' error(「Application-defined或對象deffined錯誤「) – Nataniell