+ Reply to Thread
Results 1 to 8 of 8

array to fill combo box

Hybrid View

kieranbop array to fill combo box 09-14-2011, 04:18 AM
kieranbop Re: array to fill combo box 09-14-2011, 04:40 AM
Bob Phillips Re: array to fill combo box 09-14-2011, 04:43 AM
MaczaQ Re: array to fill combo box 09-14-2011, 04:43 AM
kieranbop Re: array to fill combo box 09-14-2011, 05:01 AM
Bob Phillips Re: array to fill combo box 09-14-2011, 06:24 AM
snb Re: array to fill combo box 09-14-2011, 06:45 AM
MaczaQ Re: array to fill combo box 09-14-2011, 06:51 AM
  1. #1
    Forum Contributor
    Join Date
    09-08-2011
    Location
    Leicester, England
    MS-Off Ver
    Excel 2007
    Posts
    157

    array to fill combo box

    I'm currently having difficulties trying to fill a combo box from data on a spreadsheet. As in just one row.


    Private Sub cmbtest_Change()
    
    'Define Application Array
    Dim Arr_Application()
    
    'Size Array
    ReDim Arr_Application(100)
    
    'Declare last row in table
    Dim LastRow As Long
    LastRow = ActiveSheet.Cells(65536, 1).End(xlUp).Row
    
    'Fill Array
    Dim i As Long       'row counter
    Dim ipos As Long    'array fill counter
    i = 1
    Do While i <= LastRow
        If Cells(i, 5).Value = Me.cmbtest.Value Then
    then I'm stuck. Just don't know what to do from their to read up to the last row in a column for the combo box.

    Any help appreciated

  2. #2
    Forum Contributor
    Join Date
    09-08-2011
    Location
    Leicester, England
    MS-Off Ver
    Excel 2007
    Posts
    157

    Re: array to fill combo box

    Private Sub cmbtest_Change()
    
    'Define Application Array
    Dim Arr_Application()
    
    'Size Array
    ReDim Arr_Application(100)
    
    'Declare last row in table
    Dim LastRow As Long
    LastRow = ActiveSheet.Cells(65536, 1).End(xlUp).Row
    
    'Fill Array
    Dim i As Long       'row counter
    Dim ipos As Long    'array fill counter
    i = 1
    Do While i <= LastRow
        If Cells(i, 5).Value Then
        Me.cmbtest.Value
        ipos = ipos + 1
        End If
        i = i + 1
    Loop
    
    'Resize Application Array
    ReDim Preserve Arr_Application(ipos - 1)
    
    'Sort Application Array Alphabetically
    Dim lLoop As Long
    Dim lLoop2 As Long
    Dim str1 As String
    Dim str2 As String
    
        
    'Sort Application Analysis Array
    For lLoop = 0 To UBound(Arr_Application)
        For lLoop2 = lLoop To UBound(Arr_Application)
            If UCase(Arr_Application(lLoop2)) < UCase(Arr_Application(lLoop)) Then
                str1 = Arr_Application(lLoop)
                str2 = Arr_Application(lLoop2)
                Arr_Application(lLoop) = str2
                Arr_Application(lLoop2) = str1
            End If
        Next lLoop2
    Next lLoop
    
    'Reduce Array to Unique Values Only
    Dim Arr_Application_Unique()
    ReDim Arr_Application_Unique(ipos - 1)
    
    'Store first entry
    Arr_Application_Unique(0) = Arr_Application(0)
    
    Dim NewSize As Long
    i = 1   'reset counter
    
    Do While i <= UBound(Arr_Application)
        If Arr_Application(i) <> Arr_Application(i - 1) Then
            Arr_Application_Unique(NewSize + 1) = Arr_Application(i)
            NewSize = NewSize + 1
        End If
        i = i + 1
    Loop
    
    'Resize Unique Array
    ReDim Preserve Arr_Application_Unique(NewSize)
    
    'Populate Application ComboBox
    Me.cmbtest.List = Arr_Application_Unique
    End Sub
    editted code as it was to try and read data into a dropdown but no luck.. no errors prompted, or does anyone know an easier way to read excel information to a combo box?

  3. #3
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: array to fill combo box

    Dim Arr_Application()   'Application Array
    Dim i As Long           'row counter
    Dim LastRow As Long 'last row in table
    
    LastRow = ActiveSheet.Cells(65536, 1).End(xlUp).Row
    
    Arr_Application = Application.Transpose(Range("A1").Resize(LastRow))
    Me.cmbtest.List = Arr_Application

  4. #4
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: array to fill combo box

    notice that combobox listindex starts with index 0 whilst indexes of cells always starts from 1 - maybe it is a reason

    [tip] for fill out ComboBox you can use for example:
    ComboBox1.RowSource = "Sheet1!A1:A100"
    Best Regards
    MaczaQ

  5. #5
    Forum Contributor
    Join Date
    09-08-2011
    Location
    Leicester, England
    MS-Off Ver
    Excel 2007
    Posts
    157

    Re: array to fill combo box

    @mac the range of the combo box can vary at any given time

    @bob the code you sent me alone enough to have the combo box drop down for column 5? or what else would be needed for it to read that?

  6. #6
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: array to fill combo box

    Change A1 to E1.

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: array to fill combo box

    Probably this suffices:

    Sub snb()
      combobox1.list=columns(5).specialcells(2).value
    End sub
    or

    Sub snb2()
      combobox1.list=cells(1,5).resize(cells(rows.count,5).end(xlup).row).value
    End Sub
    Last edited by snb; 09-14-2011 at 06:48 AM.



  8. #8
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: array to fill combo box

    it could be also dynamic deffined Range
    'variable for set-up
    Sh = "Sheet1"
    first = 1
    last = 100
    col = "E"
    
    ComboBox1.RowSource = Sh & "!" & col & first & ":" & col & last
    BR
    MaczaQ

+ 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