PDA

View Full Version : Super Quick ComboBox Question



Saladsamurai
10-13-2009, 09:35 AM
Hi all. I am just messing around with VBA today trying to learn some new stuff here.

I am onto ComboBoxes. I have placed one in a worksheet using the Controls
toolbar.

I have copied this lame code that I found online just to get the idea of how it works.

Option Explicit

Private Sub ComboBox1_Change()
Select Case ComboBox1.Text
Case "Dogs"
MsgBox "Dogs"
Case "Cats"
MsgBox "Cat"
Case "Mice"
MsgBox "Mice"
Case Else
End Select
End Sub

Private Sub Worksheet_Activate()
ComboBox1.Clear
ComboBox1.AddItem "Dogs"
ComboBox1.AddItem "Cats"
ComboBox1.AddItem "Mice"
ComboBox1.Text = ComboBox1.List(0)
End Sub



What does the bold line instruct Excel to do?

I get that it is a WorkSheet Event code in which it clears (unpopulates)
alll of the drop-down options. Then it repopulates them with the AddItem thingy. But what does "ComboBox1.Text = ComboBox1.List(0)" do?

Thanks!

Saladsamurai
10-13-2009, 09:40 AM
Okay! I think I get it. That is what it instructs to display. What if I want it to be blank?

Not that I ever would.....just curious (just in case)....

georgiboy
10-13-2009, 10:16 AM
Maybe replace...
ComboBox1.Text = ComboBox1.List(0)
with
ComboBox1.Text = ""

Bob Phillips
10-13-2009, 12:18 PM
How about just



ComboBox1.ListIndex = -1