VBA Enter key problem

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
778
Office Version
  1. 2016
Platform
  1. Windows
I have a cell with a validation drop down. I also have the code below that applies formatting to a range depending on the value chosen in the validation cell.

The problem is that validation enters the value. But doesn’t “Press Enter” afterwards. So the vba won’t pick up the value and run.

I’ve tried using a separate combo box, SendKeys ~, SendKeys ENTER etc but still can’t think of a way to do it.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" And Target = "Dogs" Then
Range("Row18").Select
Selection.NumberFormat = "0.00%"
End If
If Target.Address = "$B$2" And Target = "Cats" Then
Range("Row18").Select
Selection.NumberFormat = "0"
End If
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Changing a drop-down should trigger the event handler. Are you sure it isn't?

Insert MsgBox Target.Address in as the first executable statement and try again - make sure it reports $B$2 back at you.

Change another cell - for example A1. Does it get triggered for that?

Is the event handler in the code module for that sheet?
 
Upvote 0
Changing the value in a Data Validation drop-down list does trigger the Worksheet_Change event. So your code should work. It did for me. You don't have to "Enter" in the change. Just select a value from the drop-down list.

I updated your code below. It's essentially the same as your original.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$B$2" Then
        Select Case Target.Value
            Case "Dogs":  Range("Row18").NumberFormat = "0.00%"
            Case "Cats":  Range("Row18").NumberFormat = "0"
        End Select
    End If
    
End Sub

This assumes "Row18" is a named range and not necessarily the actual row 18.

Make sure you haven't left the Event macros "Turn off". As a test, run the following macro to ensure the events are "On". Then try changing the the drop-down list.
Code:
Sub Events_On()
    Application.EnableEvents = True
End Sub
 
Upvote 0
Hi,

Thanks for the replies, much appreciated.

I tried inserting MsgBox Target.Address and it returns the msgbox, for both values. So my error must be the format side. I'll have to use something else.
 
Upvote 0
What's "Row18"? A named range? I mean, have you actually defined a range with that name?
 
Upvote 0
Row18 isn't the best range name I've made, I have to say. The range is actually F18:HF18.

Can't understand why it won't appply the simply formatting bit of code.
 
Upvote 0
The formatting looks okay to me. I assume the cats/dogs thing isn't your actual code though. Is it possible that Target.Value isn't ever "Cats" or "Dogs" (or whatever the real data is)?

String comparisons in VBA are case sensitive, so try:-
Code:
        Select Case UCase(Target.Value)
            Case UCase("Dogs"):  Range("Row18").NumberFormat = "0.00%"
            Case UCase("Cats"):  Range("Row18").NumberFormat = "0"
        End Select

Alternatively insert Options Compare Text at the top of your code module. This makes string comparisons non-case-sensitive throughout the module.

Do you know how to use breakpoints? Add one at the start of your procedure and step through the code using the F8 key, watching which statements are being executed and what the values in your variables are. Make sure when you get "Dogs", the statement Range("Row18").NumberFormat = "0.00%" is being highlighted: if it isn't, then there's a discrepancy between the data you have and the data you think you have, for example "Dogs" and "Dogs " (trailing space).Mousing over Target.Value will reveal that.

To disregard leading and trailing spaces, change your Select line to:-
Code:
        Select Case UCase(Trim(Target.Value))

If you're expecting your data to always trigger one of the Case statements, you could add a Case Else immediately before the End Select which will highlight the fact that some data didn't trigger any of the Case statements:-
Code:
            Case Else: MsgBox "Oops! """ & Target.Value & """ fell through all my case statements!"

Persevere - this won't be insoluble!
 
Last edited:
Upvote 0
Well this got me very confused. If I use your code:

Select Case UCase(Target.Value)
Case UCase("Dogs"): Range("Row18").NumberFormat = "0.00%"
Case UCase("Cats"): Range("Row18").NumberFormat = "0"
End Select

In a new workbook it works perfectly. But not in the current workbook. But if the current workbook is open the code won't work in the new one. Eh!

The code sheet in the current workbook has other codes but they all relate to buttons on the worksheet.
 
Upvote 0
If you have mutliple workbooks open, you need to make sure that any references to rows/columns/cells/ranges are fully qualified, i.e. that they point to the exact place you expect them to be pointing and that they're not pointing to whatever worksheet happens to be active at the time the code runs. In fact it's good practice to do that all of the time.

So for example, rather than Range("Row18") you should always code ThisWorkbook.Sheets("Sheet1").Range("Row18"). (Insert the correct sheet name.)
 
Upvote 0
Hi,

Thanks once again for the reply.

I have changed the references to be exact. As the code works perfectly on any workbook apart from the one. I think it's time to transfer everything into a new workbook.

I really appreciate your help and hope you have a great weekend.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top