User form question

H

Haas

Hi all,

I have created a userform with a list of dates to choose from in a List Box;
the user can choose only one date, but in case they don't, there is always a
default date selected, being the last item on the list. How can I achieve
this?

Thanks much in advance!
 
C

Chip Pearson

Haas,

Try something like the following:

Dim Result As String
Dim ResultDate As Date
With Me.ListBox1
If .ListIndex < 0 Then ' none selected
Result = .List(.ListCount - 1)
Else
Result = .List(.ListIndex)
End If
End With
ResultDate = DateValue(Result)
MsgBox ResultDate


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
H

Harald Staff

Hi

Set the style to "list" and make sure something (=last item) is selected before display.
Something like

Private Sub UserForm_Initialize()
' This is it part 1 :
ComboBox1.Style = fmStyleDropDownList
' List fill. Whatever.
ComboBox1.AddItem "1"
ComboBox1.AddItem "2"
ComboBox1.AddItem "3"
ComboBox1.AddItem "4"
' This is it pt 2 :
ComboBox1.ListIndex = ComboBox1.ListCount - 1
End Sub
 
J

J.E. McGimpsey

in your userform initialization code:

ListBox1.ListIndex = ListBox1.ListCount - 1
 
H

Harald Staff

Doh ! Listbox of course. My mistake, sorry, I was thinking combobox'ed. Once
something is preselected in a listbox then something will always be, as in
the other replies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top