+ Reply to Thread
Results 1 to 12 of 12

Fill listbox with additem

  1. #1
    Registered User
    Join Date
    06-22-2018
    Location
    suisse
    MS-Off Ver
    office 2016
    Posts
    59

    Fill listbox with additem

    Hello,
    I have a list box with 28 collumns and I would like to use the listbox add item function and don't add certain items based on a value I have found this code but don't know how to add the values from other collumns
    Please Login or Register  to view this content.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,915

    Re: Fill listbox with additem

    You can't use AddItem if you want 28 columns. You have to populate an array with the data and then assign that to the listbox. Which columns are you trying to add?
    Rory

  3. #3
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Fill listbox with additem

    Or try this

    PHP Code: 
    Private Sub UserForm_Initialize()
        
        
    Dim m As Long
        Dim myrange 
    As Range
        Dim wsh 
    As Worksheet
        Set wsh 
    Worksheets("Suivi2")
        
    wsh.Range("A" wsh.Rows.Count).End(xlUp).Row
        Me
    .ListBox1.RowSource ""
        
    With wsh.Sort
            
    .SortFields.Clear
            
    .SortFields.Add Key:=Range("AB2:AB" m), SortOn:=xlSortOnValuesOrder:=xlAscendingDataOption:=xlSortNormal
            
    .SortFields.Add Key:=Range("I2:I" m), SortOn:=xlSortOnValuesOrder:=xlAscendingDataOption:=xlSortNormal
    '
            .SetRange Range("A2:AB" & m)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        With wsh
    '        
    .AutoFilter
            
    .Range("A2:AB" m).AutoFilter Field:=9Criteria1:="<>NA"_
            Operator
    :=xlAnd
            
    .Range("A2:AB" m).AutoFilter Field:=28Criteria1:="=10"_
            Operator
    :=xlAnd
        End With
        Set myrange 
    wsh.Range("A2:AB" Cells(Rows.Count"A").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
        
    Me.ListBox1.RowSource myrange.Address
        
    End Sub 
    Teach me Excel VBA

  4. #4
    Registered User
    Join Date
    06-22-2018
    Location
    suisse
    MS-Off Ver
    office 2016
    Posts
    59

    Re: Fill listbox with additem

    It works almost exactly as i wanted! Perfect! thank you very much, now if i want to extend it to 29 or 30 collumns when i change some value it gives me an error perhaps i'm doing something wrong :-(

  5. #5
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Fill listbox with additem

    Taking the full advantage of unused Excel cells.

    PHP Code: 
    Private Sub UserForm_Initialize()
        
        
    Dim m As LongAs Long
        Dim myrange 
    As Range
        Dim wsh 
    As Worksheet
        
        Set wsh 
    Worksheets("Suivi2")
        
    wsh.Range("A" wsh.Rows.Count).End(xlUp).Row
        n 
    wsh.Range("A1").End(xlToRight).Column
        Me
    .ListBox1.columncount n
        Me
    .ListBox1.RowSource ""
        
    With wsh
            With 
    .Sort
                
    .SortFields.Clear
                
    .SortFields.Add Key:=Range("AB2:AB" m), SortOn:=xlSortOnValuesOrder:=xlAscendingDataOption:=xlSortNormal
                
    .SortFields.Add Key:=Range("I2:I" m), SortOn:=xlSortOnValuesOrder:=xlAscendingDataOption:=xlSortNormal
    '
                .SetRange wsh.Range(wsh.Cells(2, 1), wsh.Cells(m, n))
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            .Range(.Cells(1, 1), .Cells(m, n)).AutoFilter Field:=9, Criteria1:="<>NA", _
            Operator:=xlAnd
            .Range(.Cells(1, 1), .Cells(m, n)).AutoFilter Field:=28, Criteria1:="=10", _
            Operator:=xlAnd
            
            Set myrange = .Range(.Cells(2, 1), .Cells(m, n)).SpecialCells(12)
            
            myrange.Copy .Range("FX1")
            Me.ListBox1.List = .Range("FX1").CurrentRegion.Value
            .Range("FX1").CurrentRegion.Clear
        End With
    End Sub 
    You may expand it to more than 30 columns without resetting Listbox's Columncount.


    Note: Please always test code on a copy of your file and if according to requirements then apply on the original file.
    Last edited by ImranBhatti; 09-04-2018 at 03:23 AM.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Fill listbox with additem

    Your code
    Please Login or Register  to view this content.
    It means match to one of 2 conditions.
    If so
    Please Login or Register  to view this content.
    If it suppose to be AND then change + to *

  7. #7
    Valued Forum Contributor dotchiejack's Avatar
    Join Date
    05-21-2015
    Location
    Antwerp,Belgium
    MS-Off Ver
    2016
    Posts
    507

    Re: Fill listbox with additem

    When you use a table for your data is this enough.
    (in this example table name is DP69_tbl)
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-22-2018
    Location
    suisse
    MS-Off Ver
    office 2016
    Posts
    59

    Re: Fill listbox with additem

    Can't i adapt to code just like this?
    Cause as i can see on your code it's copying the cells to another worksheet right?
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    06-22-2018
    Location
    suisse
    MS-Off Ver
    office 2016
    Posts
    59

    Re: Fill listbox with additem

    also with this code it filters from the A2 so the first row appears like a header i've already changed to A1 but it's the same :-(

  10. #10
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Fill listbox with additem

    Quote Originally Posted by dark_prince69 View Post
    Can't i adapt to code just like this?
    Cause as i can see on your code it's copying the cells to another worksheet right?
    Copying on the same sheet away from your original data, loading into listbox and clearing the copied data.

    Quote Originally Posted by dark_prince69 View Post
    also with this code it filters from the A2 so the first row appears like a header i've already changed to A1 but it's the same

    The workbook that I used in my experiment started from row 2.

  11. #11
    Registered User
    Join Date
    06-22-2018
    Location
    suisse
    MS-Off Ver
    office 2016
    Posts
    59
    Quote Originally Posted by dark_prince69 View Post
    Can't i adapt to code just like this?
    Cause as i can see on your code it's copying the cells to another worksheet right?
    Please Login or Register  to view this content.
    With his code it’s working correctly, but when it doesn’t finds the value it shows me the header on the listbox
    And to clear the filter I found a way but not working as excepted, I will post the workbook later so it’s better to view...

  12. #12
    Registered User
    Join Date
    06-22-2018
    Location
    suisse
    MS-Off Ver
    office 2016
    Posts
    59

    Re: Fill listbox with additem

    and also when i try to sort A values by order i need to click 2 times on the button to be able to sort it
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Error with listbox . additem
    By jkj115 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-31-2018, 10:09 PM
  2. ListBox.AddItem problem
    By randomdude in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-25-2014, 04:51 PM
  3. [SOLVED] Add 3 columns to Listbox by AddItem
    By Joakim N in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-12-2014, 05:42 AM
  4. Additem to listbox from Range
    By Biffer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-30-2014, 12:03 PM
  5. Listbox Empty When Form Loads After Using .additem
    By jclark419 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-03-2010, 11:33 AM
  6. AddItem to Column 2 in ListBox?
    By CrazyFileMaker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2009, 04:12 PM
  7. [SOLVED] AddItem with multi-column listBox
    By David in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-26-2005, 01:05 PM

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