-
Albert Kallal 4,806 Reputation points
2021-02-24T03:50:05.713+00:00 Ok, first up, let’s translate that code to English.
Give me the number of seconds since the Unix “Epoch” time.
Just like VBA as a starting time/date, so does Unix and JavaScript uses the same standard.
That time is Jan 1, 1970.
Now this MIGHT not be important here – the code MIGHT just be using the number of seconds since then as a random type thing.
But, it DOES represent a date/time. So it is a VERY good idea to do the same thing!
Ok after we get the seconds, convert that into a hexadecimal string. Java is cute (2 is binary 16 is hex!!! - you get the idea!!!).
Then take those “xxxxxxxxxxxxx” (16) of them, and swap each X out with a RANDOM hexadecimal number!!! (0-f or 0-15 in decimal).
Then, convert the whole mess to lower case.
So 5F3BD is to become 5f3bd.
Ok, this code looks to create the same string with a matching first part.
I dont' know if that first part is imporant - but that posted code DOES MEAN that the first part is in fact datetime as HEX, so we better keep that part.
So, the function that returns the same?
This seems to do the trick:
Public Function mongoObjectId() As String Dim lngT As Long lngT = DateDiff("s", DateSerial(1970, 1, 1), Now()) Dim strT As String strT = Hex(lngT) Dim i As Integer For i = 1 To 16 strT = strT & Hex(((Rnd() * 15))) Next i strT = LCase(strT) mongoObjectId = strT End Function
And thus Bob is your uncle!
Gee, I guess those days in Mom’s basement playing with that Apple II sometimes pays off! Oh how I remember those cute little hexadecimal days and writing 6502 assembler!
Regards,
Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada
Re: convert code in javascript to vba
I speak VBA but not javascript.
Can you tell us (in English) what your js code does and where/what in the VBA code is intended to be used?
'---
NLtL
https://1drv.ms/u/s!Au8Lyt79SOuhZ_2VvKCLZxz9iwI?e=vnEabM
Add_Table of Contents, Calculate Payments, Custom_Functions, Professional_Compare