I am trying to change units from ug/Kg to mg/Kg with decimal numbers. To do this I am using a function to pass the cell with the original units (ug/Kg), and returning the modified units (mg/Kg). The issue has been trying to keep the same number of significant digits. I have figured out how to calculate the sig figs, but I am having trouble modifying the cell to be the correct number of digits dynamically. The examples I see online look something like this Range(A1).NumberFormat ="0.000" But this references the cell A1, and I need it to affect the cell I'm trying to return to.
The following code below is an example of what I'm trying to do, if anyone could help I would appreciate it.
Function ToMg(rng As Range)
Dim OrigNumb As Double
Dim NewNumb As Double
OrigNumb = rng.Cells(1).Value
' Converts ug/Kg to mg/Kg
NewNumb = OrigNumb / 1000
' **Assume that this stared comment code represents code that calculates that
' **the format for the new mg/Kg cell needs to be "0.000" dynamically
' The following command is what I need help with.
' I don't know either how
' to reference the cell ToMg is returning data too, or how to correctly change
' the number format to for example "0.000"
Me.NumberFormat = "0.000"
'This passes the modified data into the cell as desired,(but sig figs are wrong)
ToMg = NewNumb
End Function
****Edit****
Test
The goal is to have the following
In my head, I see the left column (Lets say C), being called by column D. So contents of D would be "=ToMg(C1)" (where C1 is the first data entry in the left column) with the number format of D being set dynamically. I don't think any non-dynamic number formatting will work in all the cases I have outlined above.