vb to remove blanks from excel userfrom combobox

steve2115

Board Regular
Joined
Mar 17, 2014
Messages
82
Need some help modifying the below code. I need the code to also remove blanks. It currently removes duplicates correctly, however I also need it to remove blanks from the source. Any help is much appreciated
http://www.mrexcel.com/forum/images/smilies/icon_smile.gif

Code:
Dim AllCells As Range, cell As Range
    Dim NoDupes As New Collection
    Dim i As Integer, j As Integer
    Dim Swap1, Swap2, Item

    
    
    
'   The items are in Table Industry Code to Buyer Matchup[Key Desc]field
    Set AllCells = Range("Table_Industry_Code_to_Buyer_Matchup[Key Desc]")
   
   
      
    '   The next statement ignores the error caused
'   by attempting to add a duplicate key to the collection.
'   The duplicate is not added - which is just what we want!
    On Error Resume Next
    For Each cell In AllCells
        NoDupes.Add cell.Value, CStr(cell.Value)
        
'       Note: the 2nd argument (key) for the Add method must be a string
    Next cell



'   Resume normal error handling
    On Error GoTo 0

'   Sort the collection (optional)
    For i = 1 To NoDupes.Count - 1
        For j = i + 1 To NoDupes.Count
            If NoDupes(i) > NoDupes(j) Then
                Swap1 = NoDupes(i)
                Swap2 = NoDupes(j)
                NoDupes.Add Swap1, before:=j
                NoDupes.Add Swap2, before:=i
                NoDupes.Remove i + 1
                NoDupes.Remove j + 1
                
      End If
          
    Next j
    Next i

'   Add the sorted, non-duplicated items to a ListBox
    For Each Item In NoDupes
        NonIDBuySupplier.IndCode.AddItem Item
        
        Next Item
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Where you add to the collection you could use this line instead:

Code:
If Len(cell) > 0 Then NoDupes.Add cell.Value, CStr(cell.Value)
 
Last edited:
Upvote 0
Newbie to vb ... Where exactly in the code should I put?
Code:
If Len(cell) > 0 Then NoDupes.Add cell.Value, CStr(cell.Value)
 
Upvote 0
You replace the line

Code:
NoDupes.Add cell.Value, CStr(cell.Value)
with the new one
 
Upvote 0
SOLVED - Thanks Steve the Fish... Worked like a charm once I figured out where to put the code.

Code:
NoDupes.Add cell.Value, CStr(cell.Value)

replaced with
Code:
If Len(cell) > 0 Then NoDupes.Add cell.Value, CStr(cell.Value)
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,607
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top