Consulting

Results 1 to 5 of 5

Thread: Combobox populate

  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Combobox populate

    i have a toolbox combobox on my worksheet and want to populate it with the names of certain worksheets

    Im trying the following but getting errors, any ideas please

    For Each WS In ActiveWorkbook.Worksheets
    If WS.Range("D1").Value = "CRO Number" Then
           ComboBox1.AddItem = WS.Name
    End If
        Next

  2. #2
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    This works with the combobox, I had to set a explict reference to the combobox, so you may need to change the Set cb = to point to your control.

    Private Sub CommandButton1_Click()
    Dim ws As Worksheet, cb As ComboBox
    Set cb = ActiveSheet.ComboBox1
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Range("D1").Value = "CRO Number" Then
            cb.AddItem ws.Name
        End If
    Next
    End Sub

    HTH
    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  3. #3
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi gibbo,

    And here's another way of doing it:

    Private Sub ComboBox1_DropButtonClick()
    Dim N%
    Dim MyList(10, 1) '< set from 10 to however many you want
    For N = 1 To Worksheets.Count
    If Sheets(N).Range("D1") = "CRO Number" Then
    MyList(N - 1, 0) = Sheets(N).CodeName
    End If
    Next
    ComboBox1.List = MyList
    End Sub
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    hey cal

    Thanks for that works a treat

  5. #5
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Thanks also Johnski for your version, both work great

Posting Permissions

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