Katelynne
New Member
- Joined
- Mar 29, 2023
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
I have inaccurate results being returned from the Min( ) and Max( ) functions when used in VBA script. If the value being returned from the array is a whole number, these functions are returning the correct value. If not a whole number, then these functions are returning the correct-ish value, but with digit places well beyond what is present in the data, and not all zeros. The script I'm using is generating an array which I am then inputting into the Min() or Max() function. While testing, I have even tried forcing the data in the array using the Immediate window just in case the data being pulled in the table is being displayed by Excel after rounding. The provided values are max 3 digits past the decimal, but the return is 14 digits past with not all being 0's.
Excel version: O365
Example Data
Site . . . . . . BW
101 . . . . . . 3
102 . . . . . . 0.512
103 . . . . . . 1.544
103 . . . . . . 0.768
104 . . . . . . 6
Script exerpt:
Excel version: O365
Example Data
Site . . . . . . BW
101 . . . . . . 3
102 . . . . . . 0.512
103 . . . . . . 1.544
103 . . . . . . 0.768
104 . . . . . . 6
Script exerpt:
VBA Code:
Dim site as String
Dim DataCkt As Range
Dim BW() As Single
Dim MinBwdth as String
site=103
Set DataCkt = Sheet1.Range ("A2:A6") '<--The data in the above sample
SIDcount = 0
'This part of my script works without issue returning the data from the appropriate matched lines in the range.
For Each entry In DataCkt
If entry.Value = site Then
RowNo = entry.Row '<--Identify the row
SIDcount = SIDcount + 1 '<--Advance the counter
ReDim Preserve BW(0 To SIDcount - 1) '<--Add an additional array entry while preserving the current array data
BW(SIDcount - 1) = Sheet1.Cells(RowNo, 2).Value '<--Set the new array entry to the value
End If
Next entry
'Checking the values in the array
Debug.Print BW(0)
1.544
Debug.Print BW(1)
0.768
'This part is where I'm getting the inaccurate values returned.
Debug.Print WorksheetFunction.Min(BW) '<--Same results for WorksheetFunction.Min(BW(0), BW(1)) with expected result 0.768
0.76800000667572
Debug.Print WorksheetFunction.Max(BW)
1.5440000295639