变体类型在2位小数处截止,而不是4位

时间:2021-01-19 16:31:35

I have written a custom function in Excel VBA to get the last number of a specific criteria.

我在Excel VBA中编写了一个自定义函数来获取特定条件的最后一个数字。

The function is as follows:

功能如下:

Public Function GetLastIf(ByRef LookupRange As Range, ByVal MatchVal As Variant, ByVal RangeOffset As Integer) As Variant

Dim FRow As Long
Dim lastVal As Variant
For FRow = 1 To LookupRange.Rows.Count
    If LookupRange(FRow, 1) = MatchVal Then
        lastVal = Round(LookupRange(FRow, 1 + RangeOffset).Value, 4)
    End If
Next

GetLastIf = lastVal

End Function

The range I'm looking in has two "Jul" values in the first column. The next column are 4.2641 and 4.2602, respectively, so the desired output should be 4.2602. This function (GetLastIf(range, "Jul", 1)) is only spitting out 4.2600. I'm wondering if I'm misusing something.

我正在查找的范围在第一列中有两个“Jul”值。下一列分别为4.2641和4.2602,因此所需的输出应为4.2602。此函数(GetLastIf(范围,“Jul”,1))仅吐出4.2600。我想知道我是否在滥用某些东西。

1 个解决方案

#1


0  

Try

Dim lastVal As Double

Variant can sometimes gives unexpected results.

Variant有时会产生意想不到的结果。

And you may want to format instead of round.

你可能想要格式而不是圆形。

Debug.Print Format(lastVal ,"0.0000")

That will set your value to 4 decimal places.

这会将您的值设置为4位小数。

#1


0  

Try

Dim lastVal As Double

Variant can sometimes gives unexpected results.

Variant有时会产生意想不到的结果。

And you may want to format instead of round.

你可能想要格式而不是圆形。

Debug.Print Format(lastVal ,"0.0000")

That will set your value to 4 decimal places.

这会将您的值设置为4位小数。