ComboBox

S

Steph

Hello! I have a ComboBox being populated with a named range of dates
(called date). In another cell (named range "default") I have todays date.
How can I have the value in the combobox default to todays date, or
essentially default to the named range "default"? Thanks in advance!!
 
D

Dave Peterson

How about just setting the value?

me.combobox1.value = format(date,"mm/dd/yyyy")
 
S

Steph

I tried that, but I'm not sure where to put that line of code. I tried it
at the beginning of my showform code below, but errored:

Sub ShowUserForm()

ComboBox1.Value = Worksheets("Sheet2").Range("A1").Value
UserForm1.Show

End Sub
 
D

Dave Peterson

Try putting it in the _initialize procedure (behind the userform):

Private Sub UserForm_Initialize()
me.ComboBox1.Value = Worksheets("Sheet2").Range("A1").Value
'or
me.ComboBox1.Value = Worksheets("Sheet2").Range("A1").Text
'or
me.ComboBox1.Value _
= format(Worksheets("Sheet2").Range("A1").Value, "mm/dd/yyyy")
End Sub

Only use one of those, though.
 
S

Steph

None of them worked. I am so confused!!

Dave Peterson said:
Try putting it in the _initialize procedure (behind the userform):

Private Sub UserForm_Initialize()
me.ComboBox1.Value = Worksheets("Sheet2").Range("A1").Value
'or
me.ComboBox1.Value = Worksheets("Sheet2").Range("A1").Text
'or
me.ComboBox1.Value _
= format(Worksheets("Sheet2").Range("A1").Value, "mm/dd/yyyy")
End Sub

Only use one of those, though.
 
S

Steph

In the vbe, I double-clicked on the user form itself (not the buttons or
combobox) which created the following private sub:

Private Sub UserForm_Click()

End Sub

I put this line of your code in there:
Me.ComboBox1.Value _
= Format(Worksheets("Sheet2").Range("A1").Value, "mm/dd/yyyy")

I then ran the sub I created to show the userform:
Sub ShowUserForm()
UserForm1.Show
End Sub

The userform shows, but the combobox is not populated with the default value
in Worksheets("Sheet2").Range("A1").
 
D

Dave Peterson

Don't use _click. Use _initialize.

In the vbe, I double-clicked on the user form itself (not the buttons or
combobox) which created the following private sub:

Private Sub UserForm_Click()

End Sub

I put this line of your code in there:
Me.ComboBox1.Value _
= Format(Worksheets("Sheet2").Range("A1").Value, "mm/dd/yyyy")

I then ran the sub I created to show the userform:
Sub ShowUserForm()
UserForm1.Show
End Sub

The userform shows, but the combobox is not populated with the default value
in Worksheets("Sheet2").Range("A1").
 

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