Validation VBA function

b.vasilescu

New Member
Joined
Sep 8, 2011
Messages
2
Hello all,
this is my first post here and i would appreciate any help or suggestions from you. ok then..

i have a validation function written in vba. the function does one thing it validates a fixed 13digit number. in case of a shorter or longer number it displays a message.

Function Validare_CNP(CNP As String) As String
Dim i As Integer, x As Integer
Dim cnp_array(13) As Integer
If Len(CNP) <> 13 Then
Validare_CNP = "CNP-ul not 13 digits"
Else
For i = 1 To 13
cnp_array(i) = Val(Mid(CNP, i, 1))
Next i
x = (cnp_array(1) * 2 + cnp_array(2) * 7 + cnp_array(3) * 9 + cnp_array(4) * 1 + cnp_array(5) * 4 + cnp_array(6) * 6 + _
cnp_array(7) * 3 + cnp_array(8) * 5 + cnp_array(9) * 8 + cnp_array(10) * 2 + cnp_array(11) * 7 + cnp_array(12) * 9) Mod 11
If x = 10 Then
x = 1
End If
If x = cnp_array(13) Then
Validare_CNP = "Valid"
Else
Validare_CNP = "Invalid"
End If
End If
End Function

the function is bug free, and it's working fine.
let's take 1800510170088 .The function returns a valid result
if you change a digit, the result will be invalid.
ok,

what is the problem...
the problem is i cannot figure out how can i use this function in a worksheet and when i press enter in a cell from column A, the excel to show me if i've entered an invalid number.
Thank you
p.s. excuse my poor english.
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
i meant to say that after i enter that 13digits number in a cell, validation result should appear on screen in form of a message box.

what you said it's ok, but i have to enter 200-300 numbers on a column, and validation should be instant after i press enter.

i tried using a formula in data..custom validation.. formula (from menu), but it's too long.
 
Upvote 0
Unfortunately you can't use a User Defined Function in Data Validation. You could use the Worksheet_Change event procedure to perform the validation for you.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,734
Members
452,939
Latest member
WCrawford

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