How to display time in the combo box in the forms

  • Hi,


    I want to create a combo box that allows the user to select the time. The combo box should have the time in 24hrs format with increments of 30 min. Eg the contents of the combo box should be as follows


    00:00
    00:30
    01:00
    01:30
    02:00
    etc


    But I'm having difficulty putting this in the combo box. I'm new to VBA so I don't know what to declare this variable as and how to increment it by 30min. Hope someone can help me. Thanks a million.:no:

  • Hi there.


    One way to do this is to put the times you want displayed into a range of cells, say from A1 to A48, and then set the Combo Box property ListFillRange to A1:A48.


    Hope this helps.


    Regards
    Ted

  • Hi thanks for the prompt reply but is there anyway I can do this without making use of the cells. As it's going to be a user input. (User Interface)

  • If your using a user form in the VBA editor and have a combobox within the form, the below is crude but works.



    Private Sub UserForm_Initialize()
    Dim i As Integer
    For i = 0 To 24
    ComboBox1.AddItem ((i) & ":00")
    ComboBox1.AddItem ((i) & ":30")
    ComboBox1.ListIndex = 0
    Next
    End Sub


    Now, to retrieve the info in the combobox use something like...


    Sub RetreiveTime
    Dim X
    x=Format(ComboBox1.Value,"hh:mm")
    End Sub


    Let me know how it works or if it needs more tweaking



    Phil

  • Thanks Phil and Dennis,


    Both the codes are exactly what I was looking for. So thanks a million. I was just wondering is there any way you can declare a variable in VBA such that you can put values like


    00
    01
    02 into it.


    At the moment, when you declare a variable and equate it to 00
    VB will automatically change it to just 0.


    So was just wondering. Thanks once again. I'm really beginning to LOVE this site. It has always helped me when I needed it the most:flower:

  • Hey Juggy,


    You’re very welcome and I'm glad to help. I too agree that this site is a tremendous resource.


    The string variable that Dennis mentioned is exactly where I would have gone.


    Good luck on future endeavors.


    Regards,


    Phil

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!