using VBa?
Are these value being inserted manually?
What cell do you want to check?
What do you want to happen in each instance?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("B5:B23")
' Only run if one cell updated
If Target.Count > 1 Then Exit Sub
' Only run if value in designated range
If Not Intersect(Target, rng) Is Nothing Then
' Only run if odd row number updated
If Application.WorksheetFunction.IsOdd(Target.Row) Then
' Check to make sure entry is a numeric entry
If IsNumeric(Target.Value) And (Target.Value <> 0) Then
' Do nothing, entry is fine
Else
' Clear entry and display warning
Application.EnableEvents = False
Target.ClearContents
Application.EnableEvents = True
MsgBox "Entry must be non-zero numeric entry", vbOKOnly, "ERROR!"
End If
End If
End If
End Sub
Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this VBA code in the resulting VB Editor window.
This code will run automatically upon data entry. See if it does what you want.Code:Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range Set rng = Range("B5:B23") ' Only run if one cell updated If Target.Count > 1 Then Exit Sub ' Only run if value in designated range If Not Intersect(Target, rng) Is Nothing Then ' Only run if odd row number updated If Application.WorksheetFunction.IsOdd(Target.Row) Then ' Check to make sure entry is a numeric entry If IsNumeric(Target.Value) And (Target.Value <> 0) Then ' Do nothing, entry is fine Else ' Clear entry and display warning Application.EnableEvents = False Target.ClearContents Application.EnableEvents = True MsgBox "Entry must be non-zero numeric entry", vbOKOnly, "ERROR!" End If End If End If End Sub
Hmmm... that part is a bit tricky. I don't know that you can "force" a user to make an entry. We can erase the current entry if certain conditions are not met though.the only thing I want more..... is to force the user to correctly fill the cell..
the user cant go to other cells until insert a correct value
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("A1:C10")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
' Display a message when one of the designated cells has been
' changed.
If IsEmpty(Target.Value) Then
MsgBox Target.Address & "is Empty"
Exit Sub
End If
If IsNumeric(Target.Value) And (Target.Value) < 0 Then
MsgBox Target.Address & "is less than zero"
Exit Sub
End If
If IsNumeric(Target.Value) And (Target.Value) > 0 Then
MsgBox Target.Address & "is greater than zero"
Exit Sub
End If
If IsNumeric(Target.Value) And (Target.Value) = 0 Then
MsgBox Target.Address & "is equal to zero"
Exit Sub
End If
MsgBox "Cell " & Target.Address & " has changed."
End If
End Sub