Validate user entry on Worksheet_Change event

Antony

Using Excel I am having a devil of a time trying to get the included code to work. I am attempting to restrict the values entered into a cell to only "A/B/C" They can be entered in either upper or lower case. If the value is entered in lower case then this script does successfully change the value to upper case.

The coding falls over when a value other than "A/B/C" is entered.

What I am trying to achieve is the following:

  • nothing is entered and passes the sanity check
  • A is entered and passes the sanity check
  • B is entered and passes the sanity check
  • C is entered and passes the sanity check
  • a is entered and fails the sanity check, is converted to A
  • b is entered and fails the sanity check, is converted to B
  • c is entered and fails the sanity check, is converted to C
  • anything else is entered and fails the sanity check, is converted to "" (Empty)

I have remarked out the line that fails.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Intersect(Target, Range("C9:C51")) Is Nothing Then
    Else
        With Target
            If UCase(.Text) <> .Text Then
                .Value = UCase(.Text)
            ' ElseIf .Text < "A" Or .Text > "C" Then .Value = ""
            End If
        End With
    End If
End Sub

Can anyone suggest a solution to this problem ?

Gary's Student

Perhaps:

Private Sub Worksheet_Change(ByVal Target As Excel.Range) If Intersect(Target, Range("C9:C51")) Is Nothing Then Else With Target If UCase(.Text) <> .Text Then .Value = UCase(.Text) If .Value = "A" Or .Value = "B" Or .Value = "C" Then Else .ClearContents End If End If End With End If End Sub

EDIT#1:

Based on the comment below, this should be used instead:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Intersect(Target, Range("C9:C51")) Is Nothing Then
    Else
        With Target
                Application.EnableEvents = False
                    .Value = UCase(.Text)
                    If .Value = "A" Or .Value = "B" Or .Value = "C" Then
                    Else
                        .ClearContents
                    End If
                Application.EnableEvents = True
        End With
    End If
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Avoid Worksheet_Change event if user changes a certain value

From Dev

Use DialogFragment to Validate User Entry

From Dev

Excel hyperlink follow macro after worksheet_change event

From Dev

How to manually modify a cell that is being edited in a worksheet_change event?

From Dev

Excel hyperlink follow macro after worksheet_change event

From Dev

Is it possible to automatically disable the Excel Worksheet_Change event whilst processing the same event?

From Dev

Is it possible to automatically disable the Excel Worksheet_Change event whilst processing the same event?

From Dev

How to change cell value using Worksheet_change event without triggering a second call

From Dev

How to execute Macro code inside a module from a worksheet_change event

From Dev

Worksheet_change event handler changes a cell; how to stop it triggering itself?

From Dev

Worksheet_change event handler changes a cell; how to stop it triggering itself?

From Dev

Excel form control macro updates another sheet, Worksheet_Change event does not trigger

From Dev

change cursor location in click event on tkinter entry

From Dev

Validate entry of an input box

From Dev

validate the entry of an ASCII character

From Dev

CLIPS Validate Text Entry

From Dev

Using "su - " to change user gives "No passwd entry for user"

From Dev

Worksheet Change Event - Hidden Columns

From Dev

Data Validation and Worksheet Change Event

From Dev

Select row by user_id and any row which share that entry's event_id

From Dev

worksheet_change save in xlsx

From Dev

Text entry event?

From Dev

Eventbrite duplicate event entry

From Dev

How to properly validate file from user and detect extension change?

From Dev

Change the following event from user control to viewModel

From Dev

Check if value change event was triggered by user

From Dev

Is there a way to detect the user's password entry in sshd to warn that they must change it?

From Dev

Loop to validate user input

From Dev

DataGridView validate user input

Related Related

  1. 1

    Avoid Worksheet_Change event if user changes a certain value

  2. 2

    Use DialogFragment to Validate User Entry

  3. 3

    Excel hyperlink follow macro after worksheet_change event

  4. 4

    How to manually modify a cell that is being edited in a worksheet_change event?

  5. 5

    Excel hyperlink follow macro after worksheet_change event

  6. 6

    Is it possible to automatically disable the Excel Worksheet_Change event whilst processing the same event?

  7. 7

    Is it possible to automatically disable the Excel Worksheet_Change event whilst processing the same event?

  8. 8

    How to change cell value using Worksheet_change event without triggering a second call

  9. 9

    How to execute Macro code inside a module from a worksheet_change event

  10. 10

    Worksheet_change event handler changes a cell; how to stop it triggering itself?

  11. 11

    Worksheet_change event handler changes a cell; how to stop it triggering itself?

  12. 12

    Excel form control macro updates another sheet, Worksheet_Change event does not trigger

  13. 13

    change cursor location in click event on tkinter entry

  14. 14

    Validate entry of an input box

  15. 15

    validate the entry of an ASCII character

  16. 16

    CLIPS Validate Text Entry

  17. 17

    Using "su - " to change user gives "No passwd entry for user"

  18. 18

    Worksheet Change Event - Hidden Columns

  19. 19

    Data Validation and Worksheet Change Event

  20. 20

    Select row by user_id and any row which share that entry's event_id

  21. 21

    worksheet_change save in xlsx

  22. 22

    Text entry event?

  23. 23

    Eventbrite duplicate event entry

  24. 24

    How to properly validate file from user and detect extension change?

  25. 25

    Change the following event from user control to viewModel

  26. 26

    Check if value change event was triggered by user

  27. 27

    Is there a way to detect the user's password entry in sshd to warn that they must change it?

  28. 28

    Loop to validate user input

  29. 29

    DataGridView validate user input

HotTag

Archive