Excel VBA舍入值

Excel VBA舍入值,excel,count,digits,vba,Excel,Count,Digits,Vba,我目前正在制作一个excel电子表格,其中有数百个不同的图表。My macro根据数据系列的最小值和最大值选择y轴值。我试图将最小值和最大值四舍五入到有意义的数字。例如,如果最小值为926.43四舍五入到900,最大值为1223.21四舍五入到1300。它需要能够在最小值和最大值为个位数或以百万为单位的数据范围内工作。到目前为止,这是我的代码,它在excel中工作,但每当我在VBA中运行它时,长度总是以6结尾。有什么建议吗 Set srs = ActiveChart.SeriesColl

我目前正在制作一个excel电子表格,其中有数百个不同的图表。My macro根据数据系列的最小值和最大值选择y轴值。我试图将最小值和最大值四舍五入到有意义的数字。例如,如果最小值为926.43四舍五入到900,最大值为1223.21四舍五入到1300。它需要能够在最小值和最大值为个位数或以百万为单位的数据范围内工作。到目前为止,这是我的代码,它在excel中工作,但每当我在VBA中运行它时,长度总是以6结尾。有什么建议吗

    Set srs = ActiveChart.SeriesCollection(1)
    Min = WorksheetFunction.Min(srs.Values)
    Max = WorksheetFunction.Max(srs.Values)

    Digits = Int(Max)
    Length = Len(Digits) - 2
    MinRound = WorksheetFunction.RoundDown(Min, -Length)
    MaxRound = WorksheetFunction.RoundUp(Max, -Length)

MRound就是你要找的

Dim i as integer
i = 1076
i = Application.worksheetFunction.MRound(i, 100)
这将产生i=1100


您可以使用它来查找最接近的10、100、1000,无论是什么。

我使用两个自定义函数来替代worksheetfunction.RoundUp和worksheetfunction.RoundDown。将这两个功能放在一个模块中

Function RDown(Amount As Double, digits As Integer) As Double
          RDown = Int((Amount + (1 / (10 ^ (digits + 1)))) * (10 ^ digits)) / (10 ^ digits)
End Function

Function RUp(Amount As Double, digits As Integer) As Double
    RUp = RDown(Amount + (5 / (10 ^ (digits + 1))), digits)
End Function
RDown(262704615421,-9)=262000000000,即最小值 Rup(262704615421,-9)=263000000000,即最大值