+ Reply to Thread
Results 1 to 14 of 14

List box population that does not seem to happen

Hybrid View

  1. #1
    Registered User
    Join Date
    01-24-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    7

    List box population that does not seem to happen

    I am writing some code to have a date pre selected in a set of 3 list boxes and then when the ok button is pressed this date will be added to the excel spreadsheet instead of the date that was originally in the cell. The problem that occurs is that one of the list boxes seems to poulate with the correct number of entries but they are all blank when checking them with msgbox value.

    Below is the user form initialize and select button subs

    Sub Userform_initialize()
    
    Dim I As Integer
    Dim yrtemp As String
    Dim V As Integer
    
    
    
    'put a double column into the list box
    V = lngth - lngthclsd + Numopen ' how many audits to choose from
    Selopt = False ' check whether a audit has been selected
    
    ReDim Seldata(1 To V, 1 To 3)
    'filling the array
    Worksheets(WSnam).Activate
    For I = 2 To lngthclsd
        If Cells(I, 14) <> "" Then
            Seldata(Cells(I, 14), 1) = Cells(I, 3)
            Seldata(Cells(I, 14), 2) = Cells(I, 1)
            Seldata(Cells(I, 14), 3) = I
        End If
    Next I
    
    For I = Numopen + 1 To V
        Seldata(I, 1) = Cells(I - Numopen + lngthclsd, 3)
        Seldata(I, 2) = Cells(I - Numopen + lngthclsd, 1)
        Seldata(I, 3) = I - Numopen + lngthclsd
    Next I
    
    'displaying the array in the list box
    
    ListBox1.ColumnCount = 2
    ListBox1.List = Seldata
      
    With ListBox2
        For I = 1 To 31
            .AddItem I
        Next I
    End With
    
    With ListBox3
        .AddItem "Jan"
        .AddItem "Feb"
        .AddItem "Mar"
        .AddItem "Apr"
        .AddItem "May"
        .AddItem "Jun"
        .AddItem "Jul"
        .AddItem "Aug"
        .AddItem "Sep"
        .AddItem "Oct"
        .AddItem "Nov"
        .AddItem "Dec"
    End With
    
    With ListBox4
        .AddItem Year(Now) - 1
        .AddItem Year(Now)
        .AddItem Year(Now) + 1
        .AddItem Year(Now) + 2
    End With
    
     
    '    For I = 1 To 31
     '       ListBox5.AddItem I
      '  Next I
    
    'With ListBox5
    '    For I = 1 To 31
    '        .AddItem I
    '    Next I
    'End With
    '
    'With ListBox6
    '    .AddItem "Jan"
    '    .AddItem "Feb"
    '    .AddItem "Mar"
    '    .AddItem "Apr"
    '    .AddItem "May"
    '    .AddItem "Jun"
    '    .AddItem "Jul"
    '    .AddItem "Aug"
    '    .AddItem "Sep"
    '    .AddItem "Oct"
    '    .AddItem "Nov"
    '    .AddItem "Dec"
    'End With
    With Frame6.ListBox6
        For I = 1 To 12
            .AddItem Worksheets(WS3nam).Cells(2 + I, 2)
        Next I
    End With
    
    'With ListBox7
    ' '  For I = 1 To 4
    '      .AddItem Year(Now) - 2 + I
    '    Next I
    'End With
    
    End Sub
    
    and the select button
    
    Private Sub Commandbutton4_click()
    'select button
    Dim I As Integer
    Dim II As Integer
    
    Dim dday As Integer
    Dim mmnth As Integer
    Dim yyr As Integer
    Dim yyyr As String
    
    
    
    Selopt = True
    If ListBox1.ListIndex = -1 Then
        MsgBox ("Please Select a Audit to update")
        Selopt = False
        Exit Sub
    End If
    
    I = Seldata(ListBox1.ListIndex + 1, 3)
    Worksheets(WSnam).Activate
    TextBox1.Text = Cells(I, 5) 'if there is a completed field puts this in form
    TextBox2.Text = Cells(I, 6) 'if there is a completed field puts this in form
    TextBox3.Text = Cells(I, 7) 'if there is a completed field puts this in form
    TextBox4.Text = Cells(I, 16) 'if there is a completed field puts this in form
    'MsgBox (I)
    
    dday = Day(Cells(I, 1)) - 1
    mmnth = Month(Cells(I, 1)) - 1
    
    'Select Case Month(Cells(I, 1))
    'Case Is = 1
    '    mmnth = "Jan"
    'Case Is = 2
    '    mmnth = "Feb"
    'Case Is = 3
    '    mmnth = "Mar"
    'Case Is = 4
    '    mmnth = "Apr"
    'Case Is = 5
    '    mmnth = "May"
    'Case Is = 6
    '    mmnth = "Jun"
    'Case Is = 7
    '    mmnth = "Jul"
    'Case Is = 8
    '    mmnth = "Aug"
    'Case Is = 9
    '    mmnth = "Sep"
    'Case Is = 10
    '    mmnth = "Oct"
    'Case Is = 11
    '    mmnth = "Nov"
    'Case Is = 12
    '    mmnth = "Dec"
    'End Select
    dday = Day(Cells(I, 1)) - 1
    mmnth = Month(Cells(I, 1)) - 1
    yyr = Year(Cells(I, 1)) - Year(Now) + 1
    yyyr = Year(Cells(I, 1))
    
    'MsgBox (mmnth)
    'MsgBox (yyr)
    
    Frame6.ListBox5.Selected(dday) = True 'puts day in from value
    Frame6.ListBox6.Selected(mmnth) = True ' puts month in from value
    Frame6.ListBox7.Selected(yyr) = True ' puts year in from value
    
    
    
    
    'MsgBox (ListBox6.ListIndex)
    'MsgBox (ListBox7.ListIndex)
    MsgBox (ListBox5.Value)
    MsgBox (ListBox6.Value)
    MsgBox (ListBox7.Value)
    
    
    'ListBox5.ListIndex = dday
    'ListBox6.ListIndex = mmnth
    'ListBox7.ListIndex = yyr
    'ListBox7.Text = yyyr
    'MsgBox (ListBox5.Value)
    'MsgBox (ListBox6.Value)
    'MsgBox (ListBox7.Value)
    'MsgBox (ListBox7.ListCount)
    
    For II = 1 To 31
        ListBox5.Selected(II - 1) = True
        MsgBox ("'" & ListBox5.Value & "'")
    Next II
    
    For II = 1 To 12
        ListBox6.Selected(II - 1) = True
        MsgBox ("'" & ListBox6.Value & "'")
    Next II
    
    
    For II = 1 To 4
        ListBox7.Selected(II - 1) = True
        MsgBox ("'" & ListBox7.Value & "'")
    Next II
    
    'MsgBox (ListBox5.Value & "-" & ListBox6.Value & "-" & ListBox7.Value - 2000)
    
    End Sub
    I have tried a number of ways of getting the data into the list boxes - thus the number of comments. It also doesn't make sense that the problem with the values started with the year(listbox7) and then has moved to the month(listbox6) and the day(listbox5) on occasions Also the other date information (listbox 2,3 and 4) are fine

    Thanks for your help in advance

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: List box population that does not seem to happen

    pretty hard to tell from that code alone, especially when some of the variables used are not defined.
    for example in these line only V is defined, and that's a local variable.

    'put a double column into the list box
    V = lngth - lngthclsd + Numopen ' how many audits to choose from
    Can you post example file?
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: List box population that does not seem to happen

    May you post a workbook sample?

    Are the listboxes multi-select? Are the listboxes that do not work different from the the listboxes - it seems they may sit in a Frame control?
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  4. #4
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: List box population that does not seem to happen

    Just a quick one.. not the answer to your question.. but..

    Change this:
    With ListBox1
        .AddItem "Jan"
        .AddItem "Feb"
        .AddItem "Mar"
        .AddItem "Apr"
        .AddItem "May"
        .AddItem "Jun"
        .AddItem "Jul"
        .AddItem "Aug"
        .AddItem "Sep"
        .AddItem "Oct"
        .AddItem "Nov"
        .AddItem "Dec"
    End With
    to this:
    ListBox1.List = Application.GetCustomListContents(3)
    Last edited by apo; 01-24-2014 at 05:37 AM.

  5. #5
    Registered User
    Join Date
    01-24-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: List box population that does not seem to happen

    I seem to be having a problem adding the attachment - here is a link to a google drive file

    https://drive.google.com/file/d/0B9P...it?usp=sharing

    Hope that works

    The problem I am having is is user form 2 list boxes 5,6,7
    Attached Files Attached Files
    Last edited by TimRussell; 01-24-2014 at 05:56 AM. Reason: And this time without the protection on the vba

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: List box population that does not seem to happen

    how do we use it?

    I tried running update_audit macro but it fails on overflow variable. (I corrected the error of incorrect workbook name)

  7. #7
    Registered User
    Join Date
    01-24-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: List box population that does not seem to happen

    So take 3

    Link for the g drive
    https://drive.google.com/file/d/0B9P...it?usp=sharing
    and attachment
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    01-24-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: List box population that does not seem to happen

    Each of the 4 buttons will perform a task within the spreadsheet - the one that is proving a problem for me is the update audit - just checked and found that without any data it has an issue - will fix and resend out

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: List box population that does not seem to happen

    Sorry I must be being think but what is not working? What do we need to do in order to see the actual problem.

    If I select a date from Listbox1 and press Select, items in listbox5/6/7 get selected.

  10. #10
    Registered User
    Join Date
    01-24-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: List box population that does not seem to happen

    What i am trying to do is when I select a audit from list box 1 the day of audit is automatically selected in list boxes 5,6,7 This looks like it is happening with the items selected, the plan is to use the out put of these 3 to repopulate the date of audit cells. However when I try to get this auto selection 1 of the lists does not select and after investigation I had discovered that it would not display the values in a list using the following

    'For II = 1 To 31
    '    ListBox5.Selected(II - 1) = True
    '    MsgBox ("'" & ListBox5.Value & "'")
    'Next II
    Which seemed to be the root of my problems - as I was trying to select the item using .listindex or .selected and that produced a "" result

  11. #11
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: List box population that does not seem to happen

    Perhaps you may use .List(.Listindex) rather than .Value?

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: List box population that does not seem to happen

    When you use the .Selected() array to set the highlighted item you can not use the Value property, even though the Multiselect is set to single.

    For II = 1 To 31
        If ListBox5.Selected(II - 1) = True Then
            MsgBox ("'" & ListBox5.List(II) & "'")
        End If
    Next II
    
    For II = 1 To 12
        If ListBox6.Selected(II - 1) = True Then
            MsgBox ("'" & ListBox6.List(II) & "'")
        End If
    Next II
    
    
    For II = 1 To 4
        If ListBox7.Selected(II - 1) = True Then
            MsgBox ("'" & ListBox7.List(II) & "'")
        End If
    Next II

  13. #13
    Registered User
    Join Date
    01-24-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: List box population that does not seem to happen

    So I have just tried and using the following code - works

    As a module wide variable
    Dim adatearray(1 To 31, 1 To 3) As Variant
    
    In the initialize
    For I = 1 To 31
        adatearray(I, 1) = I
    Next I
    For I = 1 To 12
        adatearray(I, 2) = Worksheets(WS3nam).Cells(I + 2, 2)
    Next I
    For I = 1 To 4
        adatearray(I, 3) = Year(Now) - 2 + I
    Next I
    
    in the ok button sub
    Adate = adatearray(ListBox5.ListIndex + 1, 1) & "-" & adatearray(ListBox6.ListIndex + 1, 2) & "-" & adatearray(ListBox7.ListIndex + 1, 3)
    Thanks again for your help

  14. #14
    Registered User
    Join Date
    01-24-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: List box population that does not seem to happen

    Andy / Izandol
    So then I would need to use the listindex and array's to provide the values I need to populate the adate (in the ok button procedure) - I'll give that a go and see what happens - thanks for your input

+ 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. Transpose a list after population
    By C90RanMan in forum Excel General
    Replies: 3
    Last Post: 10-29-2010, 02:59 PM
  2. Conditional population of a list - possible or not...
    By s80NL in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-24-2009, 06:04 AM
  3. UserForm list population
    By RobbMiller in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2007, 11:01 PM
  4. I can't make what I want to happen, happen.
    By Thanos in forum Excel General
    Replies: 6
    Last Post: 01-16-2007, 01:29 PM
  5. population list box without repeating information
    By medicenpringles in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2005, 11:05 PM

Tags for this Thread

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