I'm sure there must be a mathematical equation or some type of code that I'm not familiar with which may be capable of generating what I'm looking for, I'm trying to find a way to convert any given whole number up to 300, ideally 9999 if possible, (no decimal places) into a unique 2-digit string. I'm okay with that string containing text characters, numbers, or symbols.
I'm trying to create my own "unique key" for a set of data of our Discounts. I'm hoping I can use this to convert our "Min Qty" field from its quantity into some kind of a identifier. My original plan, I'm having a hard time figuring out how to make, but would be something like this: a = 2^1 * 9 (2^1=2, so 18) If value is under 18, it would use "A_" where the blank is filled with the multiplier by 2 to reach this value, or 0 if it is 1. B would continue for records where the value is > 18 EG: 1 would equal A0, 17 would equal A8, 42 would equal (B means 18 whatever follows) B3 like: 18 (B=3*2)
If anyone has any tips of even something similar, I would be open to it. I would love to find a way to truncate all identifiers I'm putting into this unique key and keep them unique. Currently I have a 4-digit discount id, 4-digit date "mmyy" start date and end date, and a minimum quantity which is always between 1-400.
In case you're curious of the application, this is just for me to keep record of a unique "discount row" in our report, so when I upload into a local database, I'll have an ID I can match the data on.
EDIT per request I just need a unique key generated by number values. Currently my unique key is a concatenation of Discount ID,SKU, and Min Qty, but this takes up 16 digits. I'm hoping to shorten the Discount ID and the Min Qty, while keeping the key unique.
Discount IDs are normally something like 2000-995, 2001-43, 3031-1, 2050-100, etc. Min Qty doesn't go over 400. The SKU is also a number value, anywhere from 1-6 digits.
Currently, it's very long because it looks like: 2000-995-00216-12 for Discount ID 2000-995, SKU 00216, and Minimum of 12
I'd love to find a way to shorten this in any way possible while keeping unique keys.
CodePudding user response:
The following code will map a given whole number less than 1.296 into a two-digit unique string (consisting of the characters "0,1,2,3,..9" and "A,B,...Z" As you have 36 characters in total you have 36*36=1.296 possible combinations. That will be sufficient for 300 but not for 9.999.
Option Explicit
Const ANZ = 36
Function charOne(inpVal As Long) As String
Dim x As Long
' first charcater based on the whole fraction
' which will be 0 for 36 following numbers
x = Int(inpVal / ANZ)
x = CLng(x) ' just to avoid any floating point issues
charOne = selChar(x)
End Function
Function charTwo(inpVal As Long) As String
' second character is based on the modulo
' which will count from 0 to 35
charTwo = selChar(inpVal Mod ANZ)
End Function
Function mapTo2Chars(inpVal As Long) As String
mapTo2Chars = charOne(inpVal) & charTwo(inpVal)
End Function
Function selChar(pos As Long) As String
Dim ret As String
Select Case pos
Case Is <= 9
ret = CStr(pos)
Case Is >= 10
' A has the Ascii Code 65
ret = Chr(65 pos - 10)
End Select
selChar = ret
End Function
You can test it with
Sub testit()
Debug.Print mapTo2Chars(10)
Debug.Print mapTo2Chars(887)
End Sub
CodePudding user response:
If any character may be present, use the ascii table:
TwoCharacterText = Chr(Value \ 256) & Chr(Value Mod 256)
But how you intend to use this is not clear for me.