Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Generating vailid check digits

working on project to generate numbers that contain valid check digit
based on the following:

user types a number in to a cell (right most digit is check digit and
is labeled position 1)

the check digit is generated this way:

Step (1) starting from position 2 (and working leftward), add the
values in the even numbered positions

Step (2) Multiply the result of step 1 by 3

Step (3) Starting from position 3, add up the values in the
odd-numbered positions, skipping position 1 because it's the location
of the check digit

Step (4) Add up the results for steps 2 and 3

Step (5) The check digit is the smallest number which, added to the
result in Step 4, gives a multiple of 10

Now, my problem is that I want to be able generate a list of 10 valid
numbers based on what the user inputs into a cell..

For example, user inputs "65432105"
(5 is the check digit: ((6+4+2+0)*3) + (5+3+1)) = 45 ; 45 + 5 = 50
which is mult of 10, so 5 is valid check digit)

I need to list 10 other sequential numbers with valid check digits:

example: 65432112, 65432129,...,65432204

Also the user can input any size number (I don't need to veriy the
check digit of the input number)

any help appreciated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Generating vailid check digits

Sub Make10()
sStr = ActiveCell.Text
sStr1 = Left(sStr,len(sStr)-1)
lnum = clng(sStr1)
for i = 1 to 10
sVal(i) = lNum + i
sVal(i) = GenCheck(sVal(i))
ActiveCell(i+1).Value = sVal(i)
Next
End Sub

Public Function GenCheck(sStr As String)
Dim esum As Long, osum As Long, cnum As Long
Dim i As Long
i = 0
For j = Len(sStr) To 1 Step -1
i = i + 1
If (i + 1) Mod 2 = 0 Then
esum = esum + CLng(Mid(sStr, i, 1))
Else
osum = osum + CLng(Mid(sStr, i, 1))
End If
Next
osum = esum * 3 + osum
cnum = Application.RoundUp(osum / 10, 0) * 10 - osum
GenCheck = sStr & cnum
End Function

produced:

65432112
65432129
65432136
65432143
65432150
65432167
65432174
65432181
65432198
65432204


Hopefully those are the right check digits. They match the ones you
presented.

--
Regards,
Tom Ogilvy


"Mr. E." wrote in message
om...
working on project to generate numbers that contain valid check digit
based on the following:

user types a number in to a cell (right most digit is check digit and
is labeled position 1)

the check digit is generated this way:

Step (1) starting from position 2 (and working leftward), add the
values in the even numbered positions

Step (2) Multiply the result of step 1 by 3

Step (3) Starting from position 3, add up the values in the
odd-numbered positions, skipping position 1 because it's the location
of the check digit

Step (4) Add up the results for steps 2 and 3

Step (5) The check digit is the smallest number which, added to the
result in Step 4, gives a multiple of 10

Now, my problem is that I want to be able generate a list of 10 valid
numbers based on what the user inputs into a cell..

For example, user inputs "65432105"
(5 is the check digit: ((6+4+2+0)*3) + (5+3+1)) = 45 ; 45 + 5 = 50
which is mult of 10, so 5 is valid check digit)

I need to list 10 other sequential numbers with valid check digits:

example: 65432112, 65432129,...,65432204

Also the user can input any size number (I don't need to veriy the
check digit of the input number)

any help appreciated.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Generating vailid check digits

Thanks Tom!
very nice touch there in figuring out the next multiple of ten
(would've never thought of that)

Anyways, here's what I finally wound up using (wanted it to generate
100 valid numbers, and I needed to use Text Strings since the numbers
being inputed were too large for Excel)... I left your GenCheck
function alone since it worked perfectly! thanks again.

Dim sVal(100) As String

sStr = ActiveCell.Text
intLength = Len(sStr)

sStr = Replace(sStr, " ", "", 1) 'remove spaces from text
intLength = Len(sStr) 'length of text after
' spaces removed

sStr1 = Mid(sStr, (intLength - 3), 3) 'last three digits,
' without check digit
sStr2 = Mid(sStr, 1, (intLength - 4)) 'the rest of the digits
lNum = CLng(sStr1) 'convert the string into
' a number

'ready to generate 100 new numbers
For i = 1 To 100
sVal(i) = lNum + i 'new ending for the number

sVal(i) = GenCheck(sStr2 & sVal(i)) 'generate valid check
' digit for the number
ActiveCell(i + 1).Value = sVal(i)
Next

----
Public Function GenCheck(sStr As String)
'This function calculates the correct check digit for a given number,
'then returns that number with the check digit appended to it.

'(code from help on Excel newsgroup
Dim esum As Long, osum As Long, cnum As Long
Dim i As Long
i = 0
For j = Len(sStr) To 1 Step -1
i = i + 1
If (i + 1) Mod 2 = 0 Then
esum = esum + CLng(Mid(sStr, i, 1))
Else
osum = osum + CLng(Mid(sStr, i, 1))
End If
Next
osum = esum * 3 + osum
cnum = Application.RoundUp(osum / 10, 0) * 10 - osum
GenCheck = sStr & cnum
End Function
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA write macro change column with 3 number digits to 4 digits the James C[_2_] Excel Discussion (Misc queries) 3 January 25th 10 03:12 PM
How to get excel to check last 2 digits in a 4 digit number realtorpete Excel Discussion (Misc queries) 3 January 4th 09 08:52 PM
How to calculate the UPC Check Digits David Excel Worksheet Functions 4 May 24th 07 07:23 PM
Barcode Check Digits Doug B Excel Worksheet Functions 3 February 5th 07 05:16 PM
ISBN Check Digits Colin Vicary Excel Discussion (Misc queries) 3 August 12th 05 04:12 PM


All times are GMT +1. The time now is 11:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"