![]() |
|
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Join Date: Dec 2002
Posts: 4
|
Need help with the following. Not good with VB, please, nice and simple?
I have a 12 digit number that I enter into cell C5 manually. The 12 digits I use are made up (i.e. 000345627863). I then utilize the Luhn formula to validate a checksum of the entire number utilizing the 1st digit (read right to left i.e. 3). This check sum must be equal to a number ending in zero for the sequence of numbers to be valid. I use the =MOD(K17,10) formula to do this, where K17 is the SUM(K4:K15)(The Luhn Formula values). It then converts the entire 12 digit number into HEX utilizing DEC2HEX. I'd like to utilize the following: RANDBETWEEN(000100000000,000999999999) in cell C15 to randomly generate my number. I would like it to keep generating until it's number is validated through it's checksum at cell K17 and then stop. Then I would like it to paste that number into cell C5 for conversion into HEX. I know the F9 refreshes the random number but can I do this utilizing a macro and a Form Pushbutton to complete the entire process (generate random number, stop on number with the checksum of 0, and then paste into cell C5)? Once again not very good with with VB, so the simpler, the better. THANKS IN ADVANCE. Rick |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
Hi,
This is not a complete solution for you, but please check this UDF to verify that my Luhn Formula method is correct. If it is, then we can proceed with the rest of your request. Function CC_Check(CardNum) As String You would place this in a regular module and call the function as you would a native Excel function =CC_Check(A1) for instance. This is returning a text string, but that can easily be amended to handle what you are looking to do. _________________ Bye, Jay EDIT: The way I would imagine that the full solution would work would be to call this function from the routine, and loop until a legitimate entry was found. That might not be very efficient, but for a first stab it might do until refinement. EDIT2: An even better solution would be to generate a randon 11-digit number and then append the check digit. I would imagine that would be quite easy to do and efficient to generate a nice sized sample. [ This Message was edited by: Jay Petrulis on 2002-12-20 15:48 ] |
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
Hi,
The following macro generates 50 random 12-digit numbers (13 spaces to handle negatives) and places them in the next available row in column A. In column B is placed the DEC2HEX equivalent and in column C a check of the length of the number generated. The first digit randomly selected is between -5 and +5 to minimize the chance that the number is outside of the support of the DEC2HEX function. There is still a chance to generate a #NUM! error, but it should be somewhat infrequent. I have made both of the functions private here, but that is not critical. Finally, you may want to format the first column as text so that any leading zero digits appear. Although they shouldn't affect the hex conversion, they will impact the column C results (which can be discarded anyway). Option Explicit _________________ Bye, Jay EDIT: The following replacement of part of the code above should reduce the chance of a #NUM! conversion error. For x = 1 To n - 2 [ This Message was edited by: Jay Petrulis on 2002-12-20 18:32 ] |
|
|
|
|
|
#4 | |
|
MrExcel MVP
Join Date: May 2002
Posts: 9,703
|
Quote:
Also, SumDigit as written is a general purpose method. However, in this context, its argument can never be larger than 18. So, it can be simplified to Nbr 10 + Nbr mod 10. And, I couldn't agree more with Edit2. Generating 12 digit numbers and verifying if it satisfies Luhn's condition is incredibly wasteful. 90% of the numbers will have to be discarded -- I think.
__________________
Tushar Mehta (Microsoft MVP Excel 2000-present) Excel & PowerPoint tutorials and add-ins; custom productivity solutions for MS Office |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|