使用2列上的Target.Value验证智利RUT

时间:2019-03-13 19:35:23

标签: excel vba

我正在尝试使用math algorithm来验证智利RUT(12345678-9)正常工作,但是仅当RUT存储在一个单元中时才应用该过程。 因此,当用户输入RUT时,它将显示MsgBox,让用户知道结果。

RUT
----------
01234567-8 ' MSGBOX "THIS IS CORRECT/INCORRECT."

现在,我们的数据库将RUT分为两部分存储,RUT本身01234567和校验位-得分符号后的数字-8。 我在想一种方法,用于在更改校验位列时存储和验证RUT,但是我无法弄清楚如何使用Target.Value同时存储两个值。 我们针对数据库的excel“模板”

RUT          DV
----------------
01234567     8  ' DISPLAY ALERT AS SOON AS DV IS ADDED?

我的验证一个单元格的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Columns("C")) Is Nothing Then
    On Error Resume Next

    rute = Target.Value
    arut = UCase(Target.Value)

    Rut = Replace("0000" & Rut, ".", "", 1)
    If InStr(1, Rut, "-") > 0 Then Rut = Left(rute, InStr(1, rute, "-") - 1)
    Rut = Right(rute, 8)
    suma = 0
    For i = 1 To 8
    suma = suma + Val(Mid(rute, i, 1)) * Val(Mid("32765432", i, 1))
    Next i
    dv = 11 - (suma Mod 11)
    If dv = 10 Then dv = "K"
    If dv = 11 Then dv = 0

    'If Right(arut, 1) = CStr(dv) Then MsgBox ("Rut " & rute & " Correcto")
    If Right(arut, 1) <> CStr(dv) Then MsgBox ("Rut: " & rute & " Incorrecto.")
    If rute = "" Then MsgBox ("Campo(s) modificado(s).")
End If
End Sub

任何暗示我应该怎么做或赞赏的其他方法。

注意:由于需要,无法在单元格公式中显示

1 个答案:

答案 0 :(得分:1)

这是一个示例(省略您的检查代码)

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, rute, chk, c As Range

    Set rng = Intersect(Target, Me.Range("C:D"))

    If Not rng Is Nothing Then
        For Each c in rng.cells

            rute = c.entirerow.cells(3).value
            chk = c.entirerow.cells(4).value

            'validate rute and chk here and
            '  show msgbox if needed


        Next c   
    End If

End Sub