validating a cell value

22strider

Active Member
Joined
Jun 11, 2007
Messages
311
Hello Friends

I am looking for VBA code for validating a cell value by checking if it matches with entries in a column.

This code will be a part of my larger code in which I need to trap a possible error of user entering incorrect value in a cell. The code requires a user to type valid value in a cell and then press a button. But there is always a chance of user entering incorrect value. I was wondering if there was a way of checking if the cell value matches with at least one entry in the data column (which is in the same spreadsheet).

Thanks
Rajesh
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hello Friends

I am looking for VBA code for validating a cell value by checking if it matches with entries in a column.

This code will be a part of my larger code in which I need to trap a possible error of user entering incorrect value in a cell. The code requires a user to type valid value in a cell and then press a button. But there is always a chance of user entering incorrect value. I was wondering if there was a way of checking if the cell value matches with at least one entry in the data column (which is in the same spreadsheet).

Thanks
Rajesh
You can use a worksheet_change module for this. Here's an example where the cell you want to validate is A1 and you want the user to enter a value that matches at least one cell in the range B1:B10.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Set Target = Target.Resize(1, 1)
If Not Intersect(Target, Range("A1")) Is Nothing Then
    If Not IsNumeric(Application.Match(Target.Value, Range("B1:B10"))) Then
        MsgBox "The value entered in " & Target.Address(1, 1) & " is not valid - try again"
        Application.EnableEvents = False
        With Range("A1")
            .ClearContents
            .Select
        End With
        Application.EnableEvents = True
    End If
End If
End Sub
 
Upvote 0
Small sample of your sheet -- Cell C5 is the cell to test if it is in Column A...



Excel 2010
ABC
1251
2201
3343
4159
5281343
6489
7492
8367
9303
Sheet1



Here's the code to test...

Code:
Sub FindMyValue()
Dim LR As Long
Dim Rng As Range
LR = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range("A1:A" & LR)
If WorksheetFunction.CountIf(Rng, Range("C5")) > 0 Then
MsgBox "Value found"
Else
MsgBox "Value Not Found"
End If
End Sub
 
Upvote 0
The code worked fine. I just had to make small change, in row 5 I changed "A1:A" & LR to "A1":"A" & LR
without it I was getting WorksheetFunction error.

Thanks again
Rajesh
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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