Home » excel » excel – Multiplying two arrays – Local Decimal Seperator Issue

excel – Multiplying two arrays – Local Decimal Seperator Issue

Posted by: admin April 23, 2020 Leave a comment

Questions:

Background:

Just now, I answered a question where I had to multiply two equal sized arrays. However I notice a particular issue with my local decimal seperator.


Sample Code:

Sub Sample()

Dim arr1 As Variant, arr2 As Variant, arr3 As Variant, arr4 As Variant

arr1 = Array(1, 2, 3)
arr2 = Array(0.4, 0.3, 0.2)

'Working method for static
arr3 = Evaluate("{1,2,3}*{0.4,0.3,0.2}")

'Failing method for dynamic
arr4 = Evaluate("{" & Join(arr1, ",") & "}*{" & Join(arr2, ",") & "}")

End Sub

Issue:

Whereas arr3 would populate correctly, arr4 would not and will hold a lot of errors. Reason being my local decimal seperator.

Just a simple Debug.Print arr2(1) will return 0,4. Not much of a deal because VBA can still work with the variable that have been inputed with the native (en_us) dot as a decimal seperator.

The issue is that Debug.Print Join(arr2, ",") will return: 0,5,0,4,0,3. And as you can see the dots that VBA would natively need to work with have now all turned into commas. Which in it’s turn will not work correctly in a .Evaluate that would expect the native dots.


Question:

Any way around this issue without changing Application.DecimalSeperator or writing a string value (what I did to overcome this now) which is not exactly great in case of a dynamically retrieved array?

As per request, the linked post can be found here which basically comes down to adding a few variables to insert a written string of values, eg.: str = {0.4,0.3,0.2}


Edit:

Another way I could approach the issue is to use TEXTJOIN, which instead of Join actually retains the dots:

arr4 = Evaluate("{" & Application.TextJoin(",", True, arr1) & "}*{" & Application.TextJoin(",", True, arr2) & "}")

The problem is, not everyone will have access to the function. So while this would be exchangable to others with different local settings, they would need to have one of the latest Excel versions.

Another used method is looping through arr2, joining each element with Cstr however this would also swap the dot for a comma.

How to&Answers:

My first guess is that the reason Application.DecimalSeperator does not work in the case of arr4 is because Join() function results in a string, which is not handled as a decimal, therefore the dot is not replaced by a coma.

I assume that code below runs like this:

  1. first it converts the decimal sign from “.” to “,”, including in
    arr2
  2. However Join(arr2, “;”) results in a string
    so Application.DecimalSeparator = “.” does not work (reminder – it
    needs to recognize an array item as a number to change the decimal
    separator)

-to fix it, we can use Replace() as a workaround for string.


Sub Sample()

Dim arr1 As Variant, arr2 As Variant, arr3 As Variant, arr4 As Variant

arr1 = Array(1, 2, 3)
arr2 = Array(0.4, 0.3, 0.2)
Debug.Print arr2(1)
'Failing method for dynamic

arr4 = Application.WorksheetFunction.Transpose(Evaluate("{" & Join(arr1, ";") & "}*{" & Replace(Join(arr2, ";"), Application.DecimalSeparator, ".") & "}"))
Debug.Print Join(arr1, ";")
Debug.Print Join(arr2, ";")

End Sub