Forum Discussion

Abdullah_Shurjeel's avatar
Abdullah_Shurjeel
Copper Contributor
Mar 26, 2021
Solved

Data Validation Settings fails to apply

Hope someone can help me on this.   I have 3 cells(A1,A2,A3) for which I want only Whole Numbers like 1,2,3 and do not want decimals like 0.1 or 0.5 and neither want alpha chars so I have set Data ...
  • HansVogelaar's avatar
    Mar 26, 2021

    Abdullah_Shurjeel 

    Try this:

     

    Private Sub Worksheet_Change(ByVal Target As Range)
        'Only numeric values
        Const CELL_ADDRESS = "A1:A3" 'change range
        Dim rng As Range
        Dim f As Boolean
        If Not Intersect(Target, Range(CELL_ADDRESS)) Is Nothing Then
            For Each rng In Intersect(Target, Range(CELL_ADDRESS))
                If Not IsNumeric(rng.Value) Then
                    f = True
                ElseIf Not rng.Value = Int(rng.Value) Then
                    f = True
                End If
                If f Then
                    MsgBox "You must enter only whole numbers!", vbCritical, "Invalid Input"
                    Application.EnableEvents = False
                    Application.Undo
                    Application.EnableEvents = True
                    Exit For
                End If
            Next rng
        End If
    End Sub

Resources