ComboBox Value

rob179994

New member
Joined
Apr 9, 2014
Messages
17
Reaction score
0
Points
0
Hi

I am currently doing a project for the civil engineering company i work for in Cumbria. It involves the following roads: M6, M55, A66, A595, A590, A585. At the minute, i have a combo box with the all the roads that can be chosen and I am trying to make it so that depending on which road they choose from the combo box, something relevant will happen. Before i jump right in to this however, i was trying to simplify it and get the knowledge behind it.

To simplify it, i just tried to make a macro that made a message box appear with whatever option was chosen in the combo box when it was run.

Sub Macro1()

Select Case ComboBox1.Value

Case "M6"
MsgBox "M6"
Case "M55"
MsgBox "M55"
Case "A66"
MsgBox "A66"
Case "A595"
MsgBox "A595"
Case "A590"
MsgBox "A590"
Case "A585"
MsgBox "A585"
End Select

End Sub


I have tried searching and reading and i cant find out what i am doing wrong so i tried a different approach and simplified it even more. I tried to make it so if the M6 was selected then a message box would appear saying "M6" and with everything else, it would just bring up a blank message box (didnt know how to make it do nothing). So i entered this:

Sub Macro1()

If Sheet1.ComboBox1.Value = "M6"
Then MsgBox ("M6")
Else: MsgBox ("")
End If

End Sub


Can anyone help direct me to what i am doing wrong in both cases as i am very new to programming and advise to which way is the best way to go about this? Thanks in advance.
 
Try:
Code:
Sub Macro1()
If Sheet1.ComboBox1.Value = "M6" Then
  MsgBox ("M6")
Else
  MsgBox ("")
End If
End Sub
but it could be that Sheet1 isn't the sheet you think it is!

If the above doesn't work then try changing it to:
Code:
Sub Macro1()
If [I][B][COLOR=#ff0000]ActiveSheet[/COLOR][/B][/I].ComboBox1.Value = "M6" Then
  MsgBox ("M6")
Else
  MsgBox ("")
End If
End Sub
It could be that the combobox isn't ComboBox1 but something else. Check this by selecting it and looking at the left hand extremity of the Formula bar in the area where the selection adress/names appears. It should give the name of the box there.
If instead you see Drop Down 1 or some such then it's not an ActiveX combobox but a Forms combobox.
 
Thanks for the help, it was because the "Then" was in the wrong place!
 
Back
Top