Consulting

Results 1 to 5 of 5

Thread: Listbox default tem when the list is made up of dates

  1. #1

    Listbox default item when the list is made up of dates

    Hi

    This is doing my head in!

    if I create a simple userform and insert a listbox then on the initilize code enter:

    Private Sub UserForm_Initialize()
    With ListBox1
    .RowSource = "a1:a3"
    .Value = ListBox1.List(2)
    End With
    End Sub

    then in cell a1:a3 enter the letters A, B, and C respectively

    the form works.

    however if I change cells a1:a3 to 31/3/14, 31/3/15, 31/3/16 (three dates in the UK format) then the form does not work. it falls apart on the .value property.


    if I delete the .value property the form intiailizes but for some reason with it in it cannot identify the item at position 3 in the list to be the default selection.

    RARR!!!!

    why is this????
    Last edited by hairyfingers; 10-15-2014 at 04:54 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Private Sub UserForm_Initialize()
    With ListBox1
    .RowSource = "a1:a3"
    .TopIndex = 2
    End With
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3

    Thanks - I probably should have explaned better

    Quote Originally Posted by SamT View Post
    Private Sub UserForm_Initialize()
    With ListBox1
    .RowSource = "a1:a3"
    .TopIndex = 2
    End With
    End Sub

    thanks for responding.

    I need the list index number to be selected by default at the initialze point rather than just available for selection at the top ((the index number will vary dependant upon different variables).

    any other insights? is it because its a list of dates that breaks it, because if I format the dates to the underlying number it works fine.

  4. #4
    It's OK I've solved it.

    I needed

    .Value = (Format(Range(x), "DD/MM/YYYY"))

    where x matched the row address I wanted.

    It seems although the listbox displays the dates it is actually looking at the underlying number - so by formatting the number to the date format it worked.

    cheers

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I need the list index number to be selected by default at the initialze point
    Sorry.
    Private Sub UserForm_Initialize() 
        With ListBox1 
            .RowSource = "a1:a3" 
            .Selected(2) = True
        End With 
    End Sub
    Last edited by SamT; 10-15-2014 at 03:18 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Tags for this Thread

Posting Permissions

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