vba fucntion that valid isbn

V

vbaNOOB

Hi,

Is there any1 who have written function that check if ISBN is valid or not??

Thank you
 
J

Jay Freedman

Hi,

Is there any1 who have written function that check if ISBN is valid or not??

Thank you

Based on the algorithm at
http://www.isbn-international.org/en/userman/chapter4.html#usm4_4,
this should work:

Private Function IsValidISBN(ByVal strNumber As String) As Boolean
Dim idx As Long
Dim sum As Long
Dim checkdigit As String
Dim result As Boolean
result = False ' default

'remove any hyphens
strNumber = Replace(strNumber, "-", "")

'test length
If Len(strNumber) <> 10 Then GoTo Bye

'generate sum of products
sum = 0
For idx = 1 To 9
sum = sum + Val(Mid(strNumber, idx, 1)) * (11 - idx)
Next

'add check digit
checkdigit = Right(strNumber, 1)
If UCase(checkdigit) = "X" Then
sum = sum + 10
Else
sum = sum + Val(checkdigit)
End If

'if the sum is evenly divisible by 11
'then it is valid
result = ((sum Mod 11) = 0)

Bye:
IsValidISBN = result
End Function

This subroutine shows how you could call the function:

Sub test()
Dim reply As String

reply = InputBox("Enter ISBN to check:")
If Trim(reply) = "" Then Exit Sub

If IsValidISBN(reply) Then
MsgBox reply & " is valid"
Else
MsgBox reply & " is not valid"
End If
End Sub


Note that this won't work for the 13-digit EAN or "Bookland" codes
that are becoming more common -- see section 7.5 at
http://www.isbn-international.org/en/userman/chapter7.html for the
modifications.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top