Don't use WorksheetFunction.Text
to convert a Variant/Double
to a String
; the correct format string for it would have been "@"
, not 0
- but you don't need to involve worksheet functions, the VBA standard library already provides a VBA.Conversion
module that gives you a bunch of conversion functions, including CStr
.
But, the Len
function lives in the VBA.Strings
module, which suggests that the string conversion would be implicit anyway - doesn't hurt to make that conversion explicit using an appropriate conversion function, especially given we're dealing with what appears to be undeclared variables, that would be Variant/Double
at run-time, assuming the cell values are actually numeric values.
I cannot reproduce the problem with the provided code and values. This works for me:
Dim intermitLow As Double
intermitLow = Sheet2.Range("I8").Value - Sheet2.Range("J8").Value
Debug.Print intermitLow, Len(CStr(intermitLow))
Dim intermitHigh As Double
intermitHigh = Sheet2.Range("I8").Value + Sheet2.Range("J8").Value
Debug.Print intermitHigh, Len(CStr(intermitHigh))
Output with I8=0.014 (that non-significant trailing zero in the OP is worrying) and J8=0.0002:
0.0138 6
0.0142 6
Note that I'm explicitly qualifying the Range
member calls with a proper Worksheet
object, here Sheet2
(see CodeName: Sheet1 for everything you need to know about worksheet code names). If your code is written in a standard module, then unqualified Range
calls are implicit ActiveSheet
references, i.e. it's referring to whatever worksheet happens to be active at the time, which makes the code more frail than it needs to be - although, it doesn't appear to be the problem here, since we're working with the expected values.
Consider only reading the cell values once - no need to hit the worksheet again every time:
Dim value1 As Double 'todo use a meaningful name
'note: possible type mismatch on assignment depending on the data type of .Value
value1 = Sheet2.Range("I8").Value
Dim value2 As Double 'todo use a meaningful name
'note: possible type mismatch on assignment depending on the data type of .Value
value2 = Sheet2.Range("J8").Value
Dim intermitLow As Double
intermitLow = value1 - value2
Debug.Print intermitLow, Len(CStr(intermitLow))
Dim intermitHigh As Double
intermitHigh = value1 + value2
Debug.Print intermitHigh, Len(CStr(intermitHigh))
Avoid reusing variables/identifiers for different purposes within a procedure scope, and always declare all variables (have Option Explicit
at the top of every module forcing you to do it).