+ Reply to Thread
Results 1 to 9 of 9

Thread: Loop Worksheet Combo Boxes

  1. #1
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Loop Worksheet Combo Boxes

    Hi all,

    I am working with some worksheet combo boxes and I am having troubles with loading values into them using a loop. The code:
    For cbCnt = 1 To 2
            If cbCnt = 1 Then
                For Each rCell In qtrSht.Range(qtrSht.Cells(3, 3), qtrSht.Cells(3, 3).End(xlDown))
                    With OLEObjects("cbDt" & cbCnt)
                        .AddItem rCell.Value
                    End With
                            
                Next rCell
            End If
    Next cbCnt
    Can anyone tell me why this is be so darned stubborn?

    Supashi-bo
    Last edited by Mordred; 07-25-2011 at 09:57 AM.
    Please leave a message after the beep!

  2. #2
    Forum Guru Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    2003, 2007 and 2010
    Posts
    1,207

    Re: Loop Worksheet Combo Boxes

    Hi Mordred,
    With OLEObjects("cbDt" & cbCnt).Object
    Unless your code is in the worksheet's class module, you'll also need to qualify the OLEObjects property, eg.
    With qtrSht.OLEObjects
    Hope that helps,

    Colin

    RAD Excel Blog

    Other tutorials:
    Array Formulas | Deleting Rows with VBA

  3. #3
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: Loop Worksheet Combo Boxes

    Assuming they are ActiveX comboboxes.

  4. #4
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Loop Worksheet Combo Boxes

    They are ActiveX comboboxes. Thanks guys, I will try this right now!
    Please leave a message after the beep!

  5. #5
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Loop Worksheet Combo Boxes

    That fixed it Colin, thank you!
    With OLEObjects("cbDt" & cbCnt).Object
    was the answer!
    Much appreciated!

    Michael
    Please leave a message after the beep!

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Loop Worksheet Combo Boxes

    You don't need any loop.
    You'd better avoid additem to populate comboboxes/listboxes.
    with qtrSht
      .cbDt1.list=.cells(3,3).resize(.Cells(rows.count, 3).End(xlUp).row-2).value
      .cbDt2.list=.cells(3,5).resize(.Cells(rows.count, 5).End(xlUp).row-2).value
    end with
    Last edited by snb; 07-25-2011 at 09:54 AM.



  7. #7
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Loop Worksheet Combo Boxes

    Hi snb, why avoid .AddItem? Here is the code for a combobox clear and then refill that I am troubled by:
    Sub btClear_Click()
    
        Dim frmSht As Worksheet, qtrSht As Worksheet
        Dim lbCnt As Long, lbCntr As Long, cbCnt As Long, cbCntr As Long
        Dim rCell As Range
        Set qtrSht = Worksheets("Qrtr")
        Set frmSht = Worksheets("Sheet2")
        For cbCnt = 1 To 2
            If cbCnt = 1 Then
                frmSht.OLEObjects("cbDt" & cbCnt).Object.Clear
                For Each rCell In qtrSht.Range(qtrSht.Cells(3, 3), qtrSht.Cells(3, 3).End(xlDown))
                    If cbCnt = 1 Then
                    With frmSht.OLEObjects("cbDt" & cbCnt).Object
                            .Top = 99.75
                            .Width = 119.25
                            .Height = 19.5
                            .AddItem rCell.Value
                    End With
                    End If
                Next rCell
            End If
        Next cbCnt
        For some reason I cannot set the dimensions and the top.
    End Sub
    Last edited by Mordred; 07-25-2011 at 10:16 AM.
    Please leave a message after the beep!

  8. #8
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Loop Worksheet Combo Boxes

    Additem isn't designed for populating listboxes/comboboxes; .list is. And additem is terribly slooooooow...
    If you use .List you won't need to use .clear, because .list replaces the Listbox's whole content.
    This suffices:

    Sub btClear_Click()
      with sheets("Sheet2")
        .cbDt1.list=sheets("Qtr").columns(3).specialcells(2).offset(2).specialcells(2).value
        .cbDt2.list=.cbDt1.list
      end with
    End Sub



  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: Loop Worksheet Combo Boxes

    Quote Originally Posted by snb View Post
    Additem isn't designed for populating listboxes/comboboxes
    Really? On what do you base that statement?

+ 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.2.0