I have the following macro, but instead of MsgBox I want to color the cell and stop the macro:
Sub verificaCNP()
'
' verificaCNP Macro
'
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
For Each c In Range("G3:G" & lr).Cells
If c.Value <> "" Then
c.Activate
ActiveCell.Interior.ColorIndex = xlColorIndexNone
If Len(ActiveCell.Value) <> 13 Then
MsgBox (ActiveCell.Value & ": CNP incorect - nu are 13 caractere")
ActiveCell.Interior.ColorIndex = 3
ActiveCell.Activate
'Exit Sub
Else
If Not VerificCnp(ActiveCell.Value) Then
MsgBox (ActiveCell.Value & ": CNP incorect.")
ActiveCell.Interior.ColorIndex = 3
ActiveCell.Activate
'Exit Sub
End If
End If
End If
Next c
End Sub
'Functie de verificare a cnp-ului
Function VerificCnp(cnp As String) As Boolean
Dim X(13) As Integer
If Val(cnp) <> 0 And Len(cnp) = 13 Then
For i = 1 To 13
X(i) = Val(Mid(cnp, i, 1))
Next i
rest = (X(1) * 2 + X(2) * 7 + X(3) * 9 + X(4) * 1 + X(5) * 4 + X(6) * 6 + X(7) * 3 + X(8) * 5 + X(9) * 8 + X(10) * 2 + X(11) * 7 + X(12) * 9) Mod 11
If (rest < 10 And rest = X(13)) Or (rest = 10 And X(13) = 1) Then
result = True
Else
result = False
End If
Else
result = False
End If
VerificCnp = result
End Function
Sub verificaCNP()
'
' verificaCNP Macro
'
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
For Each c In Range("G3:G" & lr).Cells
If c.Value <> "" Then
c.Activate
ActiveCell.Interior.ColorIndex = xlColorIndexNone
If Len(ActiveCell.Value) <> 13 Then
MsgBox (ActiveCell.Value & ": CNP incorect - nu are 13 caractere")
ActiveCell.Interior.ColorIndex = 3
ActiveCell.Activate
'Exit Sub
Else
If Not VerificCnp(ActiveCell.Value) Then
MsgBox (ActiveCell.Value & ": CNP incorect.")
ActiveCell.Interior.ColorIndex = 3
ActiveCell.Activate
'Exit Sub
End If
End If
End If
Next c
End Sub
'Functie de verificare a cnp-ului
Function VerificCnp(cnp As String) As Boolean
Dim X(13) As Integer
If Val(cnp) <> 0 And Len(cnp) = 13 Then
For i = 1 To 13
X(i) = Val(Mid(cnp, i, 1))
Next i
rest = (X(1) * 2 + X(2) * 7 + X(3) * 9 + X(4) * 1 + X(5) * 4 + X(6) * 6 + X(7) * 3 + X(8) * 5 + X(9) * 8 + X(10) * 2 + X(11) * 7 + X(12) * 9) Mod 11
If (rest < 10 And rest = X(13)) Or (rest = 10 And X(13) = 1) Then
result = True
Else
result = False
End If
Else
result = False
End If
VerificCnp = result
End Function