Cell to only allow certain text strings.

KyleJackMorrison

Board Regular
Joined
Dec 3, 2013
Messages
107
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello,

I need help with a code that will only allow certain text strings in the cell. So like data validation however not so restricted.
For example restrict the cell to have these:
Holiday
Course
Leave

But! I would like users to be allowed to enter other information before or after. For example:
"Christmas - Holiday" = Allowed
"Holiday - Bahamas" = Allowed
"First Aid Course" = Allowed
"Pineapples" = Not allowed
"Sick Days Off" = Not allowed

Many Thanks!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Put the following code in the events of your sheet

Change data in red by your information


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("[COLOR=#ff0000]B:B[/COLOR]")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Target.Value = "" Then Exit Sub
        If Target.Row < [COLOR=#ff0000]2 [/COLOR]Then Exit Sub   'row 1 with headers
                
        vals = Array("Holiday", "Course", "Leave")
        For i = 0 To UBound(vals)
            If InStr(1, Target.Value, vals(i)) > 0 Then
                exists = True
                Exit For
            End If
        Next
        If exists = False Then
            MsgBox "Text not allowed"
            Target.Value = ""
            Target.Select
        End If
    End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Last edited:
Upvote 0
You can do it with Data Validation. Let's say you have your words in A1:A3, and the cell you want to check is B1. Then select B1, click Data Validation > Custom > and enter:

=OR(ISNUMBER(SEARCH(A1:A3,B1)))

Case insensitive. If you want more words, or even a variable amount of words based on the list, we can adapt the formula to accommodate.
 
Upvote 0
Awesome works like a charm.

Anyway I can get it so it's not case sensitive? :)

Change this:

Code:
[COLOR=#333333]If InStr(1, Target.Value, vals(i)) > 0 Then[/COLOR]

By:

Code:
If InStr(1, LCase(Target.Value), LCase(vals(i))) > 0 Then
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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