I have a rather simple macro which resets the current worksheet to a predefined starting state. How this is achieved is that certain cell values are first stored in local variables:
Dim callingsheet As String
callingsheet = ActiveSheet.Name
Dim millidtemp As String
Dim eqmtidtemp As String
If callingsheet <> LoadMeasurementTemplateSheetName Then
millidtemp = millidinputrng.Value
eqmtidtemp = eqmtidinputrng.Value
End If
Here millidinputrng and eqmtidinputrng are global-scope range variables, both referring only to a single cell. After this, the sheet is completely wiped clean of any values, functions, formatting, etc.
Then the global variables are reset (including millidinputrng and eqmtidinputrng), and after this the sheet is re-initialized based on the predefined start state of that sheet, where millidinputrng and eqmtidinputrng are again assigned their proper ranges.
Finally, I try to reinsert the previously copied temporary values back in the correct ranges:
If callingsheet <> LoadMeasurementTemplateSheetName Then
millidinputrng.Value = millidtemp
eqmtidinputrng.Value = eqmtidtemp
End If
However, the assignment somehow fails. The local variable eqmtidtemp clearly shows that the value stored in it is "753362.001", which is correct. But after assigning that value back to the range, the copied value in the cell is "753362,001" - i.e. the dot changed to a comma. The same happens if I rerun the macro: again, the value is copied correctly to the local variable, but during reassignment, it then changes to "753362001", dropping the comma altogether.
Am I missing something about assigning values from variables to ranges / cells here? Why aren't the values in local variables copied to the ranges / cells exactly as they are? I tried forcing the format of the cells to "General", but with no luck.
EDIT --
As FaneDuru and Ron Rosenfeld pointed out, my assumption that the number format of the receiving range would be determined implicitly by the type of the temporary variables - string in this case - was wrong. Excel interpreted these values as numeric and stored the values in my local numeric format, with comma as decimal separator. This fixed the problem:
If callingsheet <> LoadMeasurementTemplateSheetName Then
millidinputrng.NumberFormat = "@"
eqmtidinputrng.NumberFormat = "@"
millidinputrng.Value = CStr(millidtemp)
eqmtidinputrng.Value = CStr(eqmtidtemp)
End If
millidtemp = millidinputrng.text. - SJRmillidinputrng.Value = CStr(millidtemp). What format would you like to be in the cell?TextorGeneral? - FaneDuruText(or@) before you paste the value. - Ron Rosenfeldmillidinputrng.NumberFormat = "@", followed by your codemillidinputrng.Value = CStr(millidtemp). It should work in this way. - FaneDuru