+ Reply to Thread
Results 1 to 7 of 7

Thread: Pre-Setting ComboBox Value

  1. #1
    Registered User
    Join Date
    06-25-2010
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    34

    Pre-Setting ComboBox Value

    I have a UserForm which asks for a date with three separate ComboBox (month, day, year).

    I have initialized them as follows:

    Public Sub UserForm_Initialize()
    
    ' Add months to the ComboBox
    With Month
        For x = 1 To 12
            .AddItem (x)
        Next x
    End With
    
    ' Add days to the combo box
    With Day
        For y = 1 To 31
            .AddItem (y)
        Next y
    End With
    
    ' Add Years to the combo box
    With Year
        For y = 2010 To 2015
            .AddItem (y)
        Next y
    End With
    
    Shift.AddItem "Day" ' Add day shift
    Shift.AddItem "Night" ' Add night shift
    
    'Month.Value = Month(Date)
    'Day.Value = Day(Date)
    'Year.Value = Year(Date)
    
    End Sub
    What I am trying to do is pre-set them with the current date. I have attempted it with the commented out code at the bottom which gives an error "Type Mismatch"

    Any ideas on how I can successfully pre-set them?

    Thanks!

    Also posted at: http://www.mrexcel.com/forum/showthr...15#post2382015
    Last edited by nbuk; 07-20-2010 at 11:22 AM.

  2. #2
    Registered User
    Join Date
    06-25-2010
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Pre-Setting ComboBox Value

    I have also tried using ListIndex but I still get the same error.

    Month.ListIndex = Month(Date)
    Day.ListIndex = Day(Date)
    Year.ListIndex = Year(Date)

  3. #3
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Pre-Setting ComboBox Value

    Why not using a combobox with all the dates of this year ?

    Combobox.List = Filter([transpose(Text(today()-(today()-date(year(today()),1,0))+row(1:365),"dd-mm-yyyy"))], "-")

  4. #4
    Registered User
    Join Date
    06-25-2010
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Pre-Setting ComboBox Value

    Putting all the dates of the year into one combobox would make it very large, no?

    I am not familiar with how this code works. Could you please explain exactly what it does?

    Thanks

    Edit:

    I tried the code and it works fine with displaying the dates, but today's date is not pre-selected, which is what I need. Is there any way to add on to that?
    Last edited by nbuk; 07-20-2010 at 11:32 AM.

  5. #5
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Pre-Setting ComboBox Value

    There's an example of using Comboboxes for dates here

    http://excel-it.com/free_addins.htm
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  6. #6
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Pre-Setting ComboBox Value

    snb,you've already been asked when posting complex code,in my opinion overly complex, to add explanations.

    You never seem to bother following up on any requests from members
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

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

    Re: Pre-Setting ComboBox Value

    Sub snb()
      With Combobox1
        .List = Filter([transpose(Text(today()-(today()-date(year(today()),1,0))+row(1:365),"dd-mm-yyyy"))], "-")
        .value=date
      End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0