Consider the following code:
Sub CombineNumbersToASTring()
' force Excel to show two decimals, even when there are trailing zeros
Range("A1", "B1").NumberFormat = "0.00"
' give some values to the previously mentioned cells
Cells(1, 1).Value = 0.1
Cells(1, 2).Value = 0.2
' combine the previous two values into one cell
Cells(1, 3).Value = "(" & Cells(1, 1).Value & ", " & Cells(1, 2).Value & ")"
End Sub
The result is erroneously
0.10 0.20 (0.1, 0.2)
although it should be
0.10 0.20 (0.10, 0.20)
So, how could I retain the NumberFormat when converting to a string in Excel VBA? The CStr method seemed not to work.
(Edit: I shall also add here that in my larger code the number of decimal places is changing from cell to another, so the decimal places can't be known from beforehand when writing the VBA code.)