Consulting

Results 1 to 10 of 10

Thread: vba combobox list on Userform has blanks

  1. #1
    VBAX Regular
    Joined
    Sep 2021
    Location
    Cedar Falls, IA
    Posts
    18
    Location

    vba combobox list on Userform has blanks

    This code works fine but my data has blanks, and this code shows them. What can I add to the code to remove them from showing up in the box?


    Private Sub UserForm_Initialize()
    Dim v, e
    With Sheets("domain.cards").Range("c4:c500")
        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.Combo_search_status.List = Application.Transpose(.keys)
    End With

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    Location
    Maybe add a check to see if e is blank:
    If Not .exists(e) And e <> vbNullString Then
        .Add e, Nothing
    End If
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    VBAX Regular
    Joined
    Sep 2021
    Location
    Cedar Falls, IA
    Posts
    18
    Location
    Quote Originally Posted by georgiboy View Post
    Maybe add a check to see if e is blank:
    If Not .exists(e) And e <> vbNullString Then
        .Add e, Nothing
    End If
    I tried it just now but it didn't work.

    I've attached my shared workbook that doesn't have any 'sensitive info.' that I imported those worksheets and UF into. Exceus the code (this is a personal workbook and its a work in process and needs cleaned)
    the code I'm working on is at the end of the Card_Data_entry_UF
    Attached Files Attached Files

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Private Sub UserForm_Initialize()
      With Sheets("domaincards")
        .Range("c4:c500").AdvancedFilter 2, , .Cells(1, 100), True
        .Columns(100).SpecialCells(4).Delete
        With .Cells(1, 100).CurrentRegion
            ListBox1.List = .Value
            .ClearContents
        End With
      End With
    End Sub
    Last edited by snb; 03-16-2022 at 06:38 AM.

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    Location
    It needed to be added as below:
    Private Sub UserForm_Initialize()    
        Dim v, e
        
        v = Sheets("domain.cards").Range("C4:C500").Value
        With CreateObject("scripting.dictionary")
            .comparemode = 1
            For Each e In v
                If Not .exists(e) And e <> vbNullString Then
                    .Add e, Nothing
                End If
            Next
            If .Count Then Me.Combo_search_status.List = Application.Transpose(.keys)
        End With
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    @Georgi

    Private Sub UserForm_Initialize()    
     sn = Sheets("domain.cards").Range("C4:C500")
    
     With CreateObject("scripting.dictionary")
       For j = 1 to ubound(sn)
         if sn(j,1) <> "" then x0 = .item(sn(j,1))
       Next
    
       If .Count Then Combo_search_status.List = .keys
     end with
    End Sub

  7. #7
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    Location
    Quote Originally Posted by snb View Post
    @Georgi

    Private Sub UserForm_Initialize()    
     sn = Sheets("domain.cards").Range("C4:C500")
    
     With CreateObject("scripting.dictionary")
       For j = 1 to ubound(sn)
         if sn(j,1) <> "" then x0 = .item(sn(j,1))
       Next
    
       If .Count Then Combo_search_status.List = .keys
     end with
    End Sub
    Hi snb,

    Looks shorter just missing one line for me
    Private Sub UserForm_Initialize()    
        Dim sn As Variant, j As Long, x0 As String
        
        sn = Sheets("domain.cards").Range("C4:C500")
        With CreateObject("scripting.dictionary")
            For j = 1 To UBound(sn)
                If sn(j, 1) <> "" Then x0 = .Item(sn(j, 1))
            Next
            If .Count Then Combo_search_status.List = .keys
        End With
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  8. #8
    VBAX Regular
    Joined
    Sep 2021
    Location
    Cedar Falls, IA
    Posts
    18
    Location
    It Works.
    Thanks georgiboy

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    @Georgi

    2 lines too much: the one you added and the redundant 'Option Explicit'.
    The test ,ifexists is also 100% redundant. see https://www.snb-vba.eu/VBA_Dictionary_en.html#L_5.3

  10. #10
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    Location
    Quote Originally Posted by snb View Post
    and the redundant 'Option Explicit'.


    I don't use that - I have the setting changed in the VBE 'Require Variable Declaration' = Checked
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •