Results 1 to 11 of 11

Thread: Simple code, But whats wrong with it? <Resolved>

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    52

    Unhappy Simple code, But whats wrong with it? <Resolved>

    Can anyone help me here, i am trying to populate a combobox from excel using a range of numbers. I have the below code, however the list is not populating, and i know this is possible, just maybe not the way i'm doing it.

    I just want it populated at runtime, so the user can select from the info contained in it.

    'appExcel.Workbooks.Open FileName:="C:\Program Files\LFhost\Maccrystal01.xls"
    appExcel.Worksheets("dblisting").Select
    cmbbx.AddItem = appExcel.Range("a1:a255")


    Additonally, is there a way to stop it from duplicating items? they can occur up to 19 times each. I only want a list of one of each value.


    As always help is alway appreciated.
    M
    Last edited by Morpheus; Oct 31st, 2002 at 10:03 PM.

  2. #2
    Addicted Member Halon's Avatar
    Join Date
    Oct 2002
    Location
    under desk choking on rage
    Posts
    228
    this may be off but do you do a .refresh after the .add?
    Soylent Green tastes like chicken

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    52
    Nop at this stage i dont, as the list is static and only gets modified about once every three months.

    Which is why i thought only refresh it once at runtime and save needles looping.

    Does that make sense?


    M.

  4. #4
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    I don't think you can add the range, I think you need to add each item one at a time . . .

    VB Code:
    1. Dim c As appExcel.Range
    2.     appExcel.Workbooks.Open FileName:="C:\Program Files\LFhost\Maccrystal01.xls"
    3.     appExcel.Worksheets("dblisting").Select
    4.     For Each c In range("a1:a255").Cells
    5.         cmbbx.AddItem  c.Text
    6.     Next c
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    52
    Thanks for that,

    Does this mean that it will add "all" values (c) to the list?

    I can find another way to sort for duplicates.

    or do i need to specify each item individually?


    Thanks for all your help.

    Nb: Yes very cute son, we have baby # 2 on the way in June next year.

  6. #6
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Yes it will add duplicates also.

    You could add the items to an array first, sort array, remove dups, and then populate combo.

    And more importantly . . .

    Congrats on baby #2 on the way!!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  7. #7
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    very useless comment ^_^

    I don't plan on having any kids, cute baby though ^_^
    asdf

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    52
    I'm not too sure about arrays, as i'm only really starting out so will keep trouping and see if there is another way to do it, but thanks that worked a treat.

    Thanks to all

  9. #9
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    You can even do it without an array . . .

    VB Code:
    1. Option Explicit
    2.  
    3. Sub LoadComboBox()
    4.    
    5.     Dim c As appExcel.range
    6.     Dim blnDuplicateFound As Boolean
    7.     Dim i As Integer
    8.    
    9.     appExcel.Workbooks.Open FileName:="C:\Program Files\LFhost\Maccrystal01.xls"
    10.     appExcel.Worksheets("dblisting").Select
    11.    
    12.     'make sure you have a list view on your form
    13.     'set the "sorted" property to "true" at design time
    14.     'we don't even need to sort an array!
    15.     'this is going to be a "behind-the-scenes" box so hide it
    16.     'by setting visible = false at design time also
    17.        
    18.     lstSort.Clear
    19.     For Each c In range("a1:a255").Cells
    20.         lstSort.AddItem c.Text
    21.     Next c
    22.        
    23.     'we need to check the list at least once
    24.     blnDuplicateFound = True
    25.    
    26.     Do While blnDuplicateFound = True
    27.         blnDuplicateFound = False
    28.         For i = 0 To lstSort.ListCount - 1
    29.             'ignore the first one!
    30.             If i <> 0 Then
    31.                 If lstSort.List(i) = lstSort.List(i - 1) Then
    32.                     'get rid of the duplicate
    33.                     lstSort.RemoveItem (i - 1)
    34.                     'we need to check the list again
    35.                     blnDuplicateFound = True
    36.                     Exit For
    37.                 End If
    38.             End If
    39.         Next i
    40.     Loop
    41.    
    42.     'all the duplicates are gone, now let's add to the combo box
    43.     cmbBx.Clear
    44.     For i = 0 To lstSort.ListCount - 1
    45.         cmbBx.AddItem lstSort.List(i)
    46.     Next i
    47.  
    48. End Sub
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    52
    You're trulty brillaint. Thanks youve saved me a heap of time.

    Thanks Again

  11. #11
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Hey man, glad I could help!


    several months ago I was doing all the asking!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

Posting Permissions

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



Click Here to Expand Forum to Full Width