Consulting

Results 1 to 7 of 7

Thread: Populate Combobox with Named Range

  1. #1
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location

    Populate Combobox with Named Range

    Hello all,

    This is im sure a simple question, but I want to populate a comboBox with a named range so rather than:
    With cboLookUp
            .List = Range("K2:K236").Value
        End With
    I want
    With cboLookUp
            .List = Range("JOBNAME").Value
        End With
    my problem is that I want "JOBNAME" to only refer to the cells that actually have a value in them so I don't get a populated comboBox with a bunch of blank values.

    Any help??
    To live is Christ... To code is cool!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You would either have to loop through the range and pick out the non-blanks, or copy the non-blanks to a new range and link to that.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    You could perform a simple loop if you'd like ...

    Dim c As Range, r As Range
    Set r = Range("JOBNAME")
    With cboLookUp
        For each c in r
        .AddItem = c.Value
        Next c
    End With
    .. or with a check for blank cells ...

    Dim c As Range, r As Range
    Set r = Range("JOBNAME")
    With cboLookUp
        For each c in r
        if c <> vbNullString Then .AddItem = c.Value
        Next c
    End With

  4. #4
    VBAX Contributor samohtwerdna's Avatar
    Joined
    Dec 2004
    Location
    Denver Colorado
    Posts
    143
    Location
    Thanks !!

    I knew it was a simple answer. "vbNullString" of course.
    To live is Christ... To code is cool!

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    You are very welcome. I'll go ahead and mark this thread as Solved then.

  6. #6
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Quote Originally Posted by samohtwerdna
    Thanks !!

    I knew it was a simple answer. "vbNullString" of course.
    Well, Actually "" & vbNullString makes not much difference...

    Refer to Optimize String Handling in Visual Basic

    http://vbaexpress.com/forum/showthread.php?p=43498

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Great point sheeeng!!

Posting Permissions

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