modify macro

Hansulet

Board Regular
Joined
Jan 24, 2013
Messages
164
Office Version
  1. 2021
Platform
  1. Windows
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
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Re: modufy macro

Simply delete the line starting MsgBox
and remove the ' from the line 'Exit Sub
 
Upvote 0
Re: modufy macro

Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,391
Messages
6,124,674
Members
449,179
Latest member
fcarfagna

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top