Results 1 to 3 of 3

Thread: ComboBox Value

  1. #1

    Question ComboBox Value



    Register for a FREE account, and/
    or Log in to avoid these ads!

    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.

  2. #2
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,418
    Articles
    0
    Excel Version
    365
    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 ActiveSheet.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.

  3. #3
    Thanks for the help, it was because the "Then" was in the wrong place!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •