how to convert from javascript to VBA

Cesar Gutierrez 21 Reputation points
2021-02-23T21:57:00.887+00:00

I have no idea how to use VBA, could somebody help me to convert this jasvaript function to a VBA function?

function mongoObjectId() {
let timestamp = (new Date().getTime() / 1000 | 0).toString(16);
return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
return (Math.random() * 16 | 0).toString(16);
}).toLowerCase();
}

{count} votes

Accepted answer
  1. 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


2 additional answers

Sort by: Most helpful
  1. Nothing Left To Lose 396 Reputation points
    2021-02-24T00:35:37.473+00:00

    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

    0 comments No comments

  2. Tom van Stiphout 1,696 Reputation points MVP
    2021-02-24T02:29:23.87+00:00

    If I had to guess: give me a 16-character random number based on the timestamp + a random number.

    Maybe the OP should read the help page on the Rnd and Randomize functions.

    0 comments No comments