+ Reply to Thread
Results 1 to 4 of 4

Populate listbox from 2 rows of cells

  1. #1
    Registered User
    Join Date
    11-28-2003
    Posts
    39

    Populate listbox from 2 rows of cells

    What i am trying to achieve is to list two ranges of cells in to seperate colums.
    But to only include non-emply cells from c6:j6.
    However the C5:J5 range is always popualted, this must also be filtered by the empty cells in range c6:j6.

    So in the end i might have: Col 1 = c6, e6, and j6. And col 2 = c5, e5, and j5

    So far i have the following code.

    ListBox1.ColumnCount = 2
    Set rng = ActiveSheet.Range("c6:j6")
    Set run = ActiveSheet.Range("c5:j5")
    For Each c In run
    For Each cell In rng
    If Not IsEmpty(cell) Then
    ListBox1.AddItem c.Value
    End If
    Next
    Next
    For Each cell In rng
    If Not IsEmpty(cell) Then
    ListBox1.AddItem cell.Value

    End If
    Next

    any help appreciated
    Last edited by mikewild2000; 04-09-2005 at 10:45 AM. Reason: small mistake

  2. #2
    Registered User
    Join Date
    11-28-2003
    Posts
    39
    i've got it sorted. Took a little time. No probs

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Mike,
    If I understand your post correctly, you want to load only non blank pairs into the ListBox as 2 columns. For Each is quick and easy, but lacks flexibility. Here is a code example to do that using a For Next loop.

    Dim I As Integer
    Dim N As Integer

    ListBox1.ColumnCount = 2

    For I = 3 To 10
    If Not IsEmpty(ActiveSheet.Cells(6, I)) Then
    With ListBox1
    .AddItem
    .List(N, I - 3) = ActiveSheet.Cells(5, I).Value
    N = N + 1
    End If
    Next I

    ListBox1.ListIndex = 0


    Hope this helps,
    Leith Ross
    Last edited by Leith Ross; 04-09-2005 at 09:40 PM.

  4. #4
    Registered User
    Join Date
    11-28-2003
    Posts
    39
    Hi Leith,

    thanks for your reply

    a little fiddling about, and this did the trick:

    ListBox2.ColumnCount = 2

    For I = 3 To 10
    Set yt = (ActiveSheet.Cells(6, I))
    If Not IsEmpty(yt) Then
    With ListBox2
    .AddItem yt.Value

    .List(N, 1) = ActiveSheet.Cells(5, I).Value
    N = N + 1
    End With
    End If
    Next I


    thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1