Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 30

Thread: Solved: AddItem help

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    Solved: AddItem help

    I have this vb

    [VBA]With Me
    With Me.CmbBoxDesc1
    .AddItem "2500"
    .AddItem "1000 BASE SX SHORT WAVE"
    .AddItem "10 FT.PATCH CABLE"
    .AddItem "10 FT.PRINTER CABLE"[/VBA]

    it continues with about 150+ items. My question is how can I have it assigned
    to 18 CmbBox's and not have to have one of these long lists for each CmbBox. Something like

    [VBA]With Me
    With Me.CmbBoxDesc1 - Me.CmbBoxDesc18[/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim ary(1 To 200)

    ary(1) = "2500"
    ary(2) = "1000 BASE SX SHORT WAVE"
    ary(3) = "10 FT.PATCH CABLE"
    ary(4) = "10 FT.PRINTER CABLE"
    'etc

    ComboBox1.List = ary
    ComboBox2.List = ary
    ComboBox3.List = ary
    'etc.
    [/vba]

    but why?

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    he wants to remove items as they are selected down the list...so if an item is selected in the first combobox...then it won't be available in any of the other comboboxes....and so on down the line...if I understand correctly.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    With a list that size, I would consider storing it in the workbook, or if that is not appropriate, in a text file. Much easier for maintenance if you have to add/delete/modify items.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    This Cmbbox will have a list of items that can be shipped so I want them to be typed the correct way so they will pick from list and the description would be correct.

  6. #6
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    mcmack that save in a file sounds great for this list will be updated often. How would I be able to make that work. How can I call that file to appear in each cmbbox?

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lucas
    he wants to remove items as they are selected down the list...so if an item is selected in the first combobox...then it won't be available in any of the other comboboxes....and so on down the line...if I understand correctly.
    where does it say that?

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I'm not sure I understood but I took it from this...which I admittedly don't understand:
    [VBA]
    With Me
    With Me.CmbBoxDesc1 - Me.CmbBoxDesc18
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    If that's not the case then why not just put them in a dynamic named range on a hidden sheet and use the rowsource? No code at all.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    what i wanted to say was if there was a way for me to just have the list entered once and distribute it through 18 cmbboxs.

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I guess I misunderstood....you can use the array/additem method that Bob has suggested..I would just hide a sheet with the data so it's with the workbook...see attached.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  12. #12
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why 18 comboboxes?

    If it's to allow the user to make multiple choices then surely one multiselect listbox would be better?

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This will allocate 10 items to each of 18 comboboxes. It uses a dynamic range to store the data.
    [vba]Private Sub UserForm_Initialize()
    Dim i As Long, j As Long, k As Long
    For i = 1 To 18
    For j = 1 To 10
    k = k + 1
    If k > Range("MyList").Cells.Count Then Exit Sub
    Me.Controls("ComboBox" & i).AddItem Range("MyList")(k)
    Next
    Next
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Ok for that to work for me I would need to change the 10 to the total items. Also change "ComboBox" to "CmbBoxDesc" right? Is there a way to use the text file like originally said?

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    And initialise k each time through.

  16. #16
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Private Sub UserForm_Initialize()
    Dim i As Long, j As Long
    Dim InputData As String
    Open "G:\MyData.txt" For Input As #1 ' Open file for input.
    Do While Not EOF(1) ' Check for end of file.
    Line Input #1, InputData ' Read line of data.
    If i Mod 10 = 0 Then j = j + 1
    Me.Controls("ComboBox" & j).AddItem InputData
    i = i + 1
    Loop
    Close #1 ' Close file.
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Ok so what I did I created the MyData.txt file entered the List put it in my C:\ Drive . Now edited your code to match my info. First gives me unable to locate file, then it gives me File already open even though it's not. How can i make this work?

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you zip and post a copy of your workbook and text file?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  19. #19
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Hope this helps

  20. #20
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Private Sub UserForm_Initialize()

    Dim i As Long, j As Long
    Dim InputData As String
    For j = 1 To 18
    Close #1
    Open "C:\MyData.txt" For Input As #1 ' Open file for input.
    Do While Not EOF(1) ' Check for end of file.
    Line Input #1, InputData ' Read line of data.
    Me.Controls("CmbBoxDesc" & j).AddItem InputData
    Loop
    Close #1 ' Close file.
    Next

    With Me
    With CmbBoxLocation
    .AddItem "ORLANDO, FL"
    .AddItem "RICHMOND, VA"
    .AddItem "ATLANTA, GA"
    .AddItem "MIAMI, FL"
    .AddItem "DAYTONA BEACH, FL"
    .AddItem "ROANOKE, VA"
    .AddItem "TALLAHASSE, FL"
    .AddItem "HOMOSASSA, FL"
    .AddItem "MACON, GA"
    .AddItem "JACKSONVILLE, FL"
    .AddItem "MEMPHIS, TN"
    .AddItem "STOCKBRIDGE, GA"
    .AddItem "FORT MYERS, FL"
    .AddItem "CHATTANOOGA, TN"
    .AddItem "KNOXVILLE, TN"
    .AddItem "ALCOA, TN"
    .AddItem "MULBERRY, FL"
    .AddItem "CHAMBLEE, GA"
    .AddItem "GREENSBORO, NC"
    .AddItem "CALIFORNIA, MD"
    .AddItem "GAMBRILLS, MD"
    .AddItem "SMYRNA, GA"
    End With
    End With

    With Me
    With CmbBoxProject
    .AddItem "DEPOT"
    .AddItem "VAM"
    .AddItem "PCAR 2007"
    .AddItem "UPGF"
    .AddItem "SERVICE REQUEST"
    End With
    End With
    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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