Decimal data validation using macros

2019-08-20 19:40发布

I have a macro enabled work sheet in which i have data validations for columns where i want to regulate the data input. I cannot use regular data validation feature in excel as it fails to function as we copy data from other sources which is the case in my requirement.So i am implementing data validations through macros . I have a scenario where one column can input only decimal data. The conditions are as follows the input data is of length 9 which constitutes only 2 decimal positions. I have partly written a macro for this validation which does not work(When i make a invalid input macro is not triggered therefore no msgbox pop up) and i am stuck at this point.Please help me out here to find a different if condition for the validation. The macro i have written is as follows:

Set AffectedCells = Intersect(Target, Target.Parent.Range("F:F"))

If Not AffectedCells Is Nothing Then

For Each Cell In AffectedCells
        If Not (Cell.Value * (10 ^ 2) Mod 10) <> 0 Then

            MsgBox "The value you entered is not valid." 

            Application.Undo 'undo insert
            Exit Sub 'stop checking after one invalid data was found.
        End If

标签: excel vba
2条回答
对你真心纯属浪费
2楼-- · 2019-08-20 19:41

Is this what you are trying? I have commented the code so you should not have a problem undrstanding it. But if you do then simply ask.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xCell As Range, AffectedCells As Range

    On Error GoTo Whoa

    Application.EnableEvents = False

    Set AffectedCells = Intersect(Target, Columns(6))

    If Not AffectedCells Is Nothing Then
        For Each xCell In AffectedCells
            '~~> Avoid cases like IP address 10.1.2.234
            '~~> Check if the number contains decimal
            If IsNumeric(xCell.Value) And _
            Int(xCell.Value) <> xCell.Value Then
                '~~> Check the position of the decimal
                '~~> Check the length of the string
                If Mid(xCell.Value, Len(xCell.Value) - 2, 1) <> "." Or _
                Len(xCell.Value) > 9 Then
                    '
                    '~~> INVALID INPUT: Do what you want
                    '

                    'MsgBox "The value you entered is not valid."
                    'Application.Undo
                    'Exit For
                End If
            End If
        Next xCell
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
查看更多
你好瞎i
3楼-- · 2019-08-20 19:49

This needs to be pasted on the sheet code space for the sheet you want the macro to run on.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim xCell As Range, AffectedCells As Range

Set AffectedCells = Intersect(Target, Target.Parent.Range("F:F"))

If Not AffectedCells Is Nothing Then
    For Each xCell In AffectedCells
            If Not (xCell.Value * (10 ^ 2) Mod 10) <> 0 Then
                MsgBox "The value you entered is not valid."
                    Application.EnableEvents = False
                        Application.Undo
                    Application.EnableEvents = True
                Exit Sub
            End If
    Next xCell
End If

End Sub
查看更多
登录 后发表回答