如何使用excel VBA round()进行汇总?

如何使用excel VBA round()进行汇总?,excel,vba,Excel,Vba,我有以下数据: cell(1,1) = 2878.75 cell(1,2) = $31.10 cell(2,1) = $89,529.13 但是,当我尝试使用舍入(单元格(1,1).value*单元格(1,2).value),2)时,结果与单元格(2,1)不匹配。我想这与取整问题有关,但我只是想知道是否有可能让round()正常工作。也就是说,对于value>0.5,四舍五入。对于值G,那么 '查找长度为G的字符串中的最后一个空格,以确保行以一个完整的单词结尾,新行以一个完整的单词开头 直到L

我有以下数据:

cell(1,1) = 2878.75
cell(1,2) = $31.10
cell(2,1) = $89,529.13

但是,当我尝试使用
舍入(单元格(1,1).value*单元格(1,2).value),2)
时,结果与
单元格(2,1)
不匹配。我想这与取整问题有关,但我只是想知道是否有可能让
round()
正常工作。也就是说,对于
value>0.5
,四舍五入。对于
值<0.5
,向下取整?

尝试向上取整功能:

Dim i As Double

i = Application.WorksheetFunction.RoundUp(Cells(1, 1).Value * Cells(1, 2).Value, 2)
VBA的使用是为了补偿总是在0.5上向上或向下取整的偏差;你可以取而代之

WorksheetFunction.Round(cells(1,1).value * cells(1,2).value, 2)


试试这个函数,可以凑一个双精度整数

'---------------Start -------------
Function Round_Up(ByVal d As Double) As Integer
    Dim result As Integer
    result = Math.Round(d)
    If result >= d Then
        Round_Up = result
    Else
        Round_Up = result + 1
    End If
End Function
'-----------------End----------------

如果你想四舍五入,使用半调整。将要四舍五入的数字加上0.5,然后使用INT()函数


answer=INT(x+0.5)

我自己也找到了解决办法:

    'G = Maximum amount of characters for width of comment cell
    G = 100
    'CommentX
    If THISWB.Sheets("Source").Cells(i, CommentColumn).Value = "" Then
        CommentX = ""
     Else
        CommentArray = Split(THISWB.Sheets("Source").Cells(i, CommentColumn).Value, Chr(10)) 'splits on alt + enter
        DeliverableComment = "Available"
    End If
                        If CommentX <> "" Then

                            'this loops for each newline in a cell (alt+enter in cell)
                            For CommentPart = 0 To UBound(CommentArray)
                            'format comment to max G characters long
                                LASTSPACE = 0
                                LASTSPACE2 = 0
                                    If Len(CommentArray(CommentPart)) > G Then

                                        'find last space in G length character string to make sure the line ends with a whole word and the new line starts with a whole word
                                        Do Until LASTSPACE2 >= Len(CommentArray(CommentPart))
                                            If CommentPart = 0 And LASTSPACE2 = 0 And LASTSPACE = 0 Then
                                                LASTSPACE = WorksheetFunction.Find("þ", WorksheetFunction.Substitute(Left(CommentArray(CommentPart), G), " ", "þ", (Len(Left(CommentArray(CommentPart), G)) - Len(WorksheetFunction.Substitute(Left(CommentArray(CommentPart), G), " ", "")))))
                                                ActiveCell.AddComment Left(CommentArray(CommentPart), LASTSPACE)
                                            Else
                                                If LASTSPACE2 = 0 Then
                                                   LASTSPACE = WorksheetFunction.Find("þ", WorksheetFunction.Substitute(Left(CommentArray(CommentPart), G), " ", "þ", (Len(Left(CommentArray(CommentPart), G)) - Len(WorksheetFunction.Substitute(Left(CommentArray(CommentPart), G), " ", "")))))
                                                   ActiveCell.Comment.Text Text:=ActiveCell.Comment.Text & vbNewLine & Left(CommentArray(CommentPart), LASTSPACE)
                                                Else
                                                   If Len(Mid(CommentArray(CommentPart), LASTSPACE2)) < G Then
                                                       LASTSPACE = Len(Mid(CommentArray(CommentPart), LASTSPACE2))
                                                       ActiveCell.Comment.Text Text:=ActiveCell.Comment.Text & vbNewLine & Mid(CommentArray(CommentPart), LASTSPACE2 - 1, LASTSPACE)
                                                   Else
                                                       LASTSPACE = WorksheetFunction.Find("þ", WorksheetFunction.Substitute(Mid(CommentArray(CommentPart), LASTSPACE2, G), " ", "þ", (Len(Mid(CommentArray(CommentPart), LASTSPACE2, G)) - Len(WorksheetFunction.Substitute(Mid(CommentArray(CommentPart), LASTSPACE2, G), " ", "")))))
                                                       ActiveCell.Comment.Text Text:=ActiveCell.Comment.Text & vbNewLine & Mid(CommentArray(CommentPart), LASTSPACE2 - 1, LASTSPACE)
                                                   End If
                                                End If
                                            End If
                                            LASTSPACE2 = LASTSPACE + LASTSPACE2 + 1
                                        Loop
                                    Else
                                        If CommentPart = 0 And LASTSPACE2 = 0 And LASTSPACE = 0 Then
                                          ActiveCell.AddComment CommentArray(CommentPart)
                                        Else
                                          ActiveCell.Comment.Text Text:=ActiveCell.Comment.Text & vbNewLine & CommentArray(CommentPart)
                                        End If
                                    End If

                            Next CommentPart
                            ActiveCell.Comment.Shape.TextFrame.AutoSize = True

                        End If
'G=注释单元格宽度的最大字符数
G=100
'评论x
如果THISWB.Sheets(“Source”).Cells(i,CommentColumn).Value=“”则
CommentX=“”
其他的
CommentArray=Split(THISWB.Sheets(“Source”).Cells(i,CommentColumn).Value,Chr(10))在alt+enter上拆分
DeliverableComment=“可用”
如果结束
如果是“X”,则
'这将循环单元格中的每个换行符(在单元格中按alt+enter)
对于CommentPart=0到UBound(CommentArray)
'将注释格式设置为最大长度为G个字符
LASTSPACE=0
LASTSPACE2=0
如果Len(CommentArray(CommentPart))>G,那么
'查找长度为G的字符串中的最后一个空格,以确保行以一个完整的单词结尾,新行以一个完整的单词开头
直到LASTSPACE2>=Len(CommentArray(CommentPart))
如果CommentPart=0,LASTSPACE2=0,LASTSPACE=0,则
LASTSPACE=WorksheetFunction.Find(“þ”,WorksheetFunction.Substitute(左(CommentArray(CommentPart),G),“”,“þ”,“Len(左(CommentArray(CommentPart),G))-Len(WorksheetFunction.Substitute(左(CommentArray(CommentPart),G),“”,“”“”))
ActiveCell.AddComment Left(CommentArray(CommentPart),LASTSPACE)
其他的
如果LASTSPACE2=0,则
LASTSPACE=WorksheetFunction.Find(“þ”,WorksheetFunction.Substitute(左(CommentArray(CommentPart),G),“”,“þ”,“Len(左(CommentArray(CommentPart),G))-Len(WorksheetFunction.Substitute(左(CommentArray(CommentPart),G),“”,“”“”))
ActiveCell.Comment.Text文本:=ActiveCell.Comment.Text&vbNewLine&Left(CommentArray(CommentPart),LASTSPACE)
其他的
如果Len(Mid(CommentArray(CommentPart),LASTSPACE2))

请随时感谢我。这对我来说很有吸引力,自动调整大小功能也很有效

我将介绍在vba中使用的两个自定义库函数,它们将用于舍入双精度值,而不是使用WorkSheetFunction.RoundDown和WorkSheetFunction.RoundUp

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(2878.75*31.1,2)将返回899529.12 函数RUp(2878.75*31.1,2)将返回899529.13 鉴于 函数Rdown(2878.75*31.1,-3)将返回89000
函数RUp(2878.75×31.1,-3)将返回90000</p>< p>数学。轮使用银行家舍入,如果要舍入的数字正好落在中间,则将舍入到最近的偶数。< /P>
简单的解决方案,使用Worksheetfunction.Round()。如果其位于边缘,则将向上取整。

这是一个示例,j是要向上取整的值

Dim i As Integer
Dim ii, j As Double

j = 27.11
i = (j) ' i is an integer and truncates the decimal

ii = (j) ' ii retains the decimal

If ii - i > 0 Then i = i + 1 

如果余数大于0,则将其四舍五入,很简单。在1.5时,它会自动取整为2,因此它将小于0。

我遇到了一个问题,我只需要取整,然后就可以了
Count = -1 * (int(-1 * x))
Function RoundDown(a As Double, digits As Integer) As Double
    RoundDown = Int((a + (1 / (10 ^ (digits + 1)))) * (10 ^ digits)) / (10 ^ digits)
End Function

Function RoundUp(a As Double, digits As Integer) As Double
    RoundUp = RoundDown(a + (5 / (10 ^ (digits + 1))), digits)
End Function

Function RDownAuto(a As Double) As Double
    Dim i As Integer
    For i = 0 To 17
        If Abs(a * 10) > WorksheetFunction.Power(10, -(i - 1)) Then
            If a > 0 Then
                RDownAuto = RoundDown(a, i)
            Else
                RDownAuto = RoundUp(a, i)
            End If
        Exit Function
        End If
    Next
End Function
RDownAuto(458.067)=458
RDownAuto(10.11)=10
RDownAuto(0.85)=0.8
RDownAuto(0.0052)=0.005
RDownAuto(-458.067)=-458
RDownAuto(-10.11)=-10
RDownAuto(-0.85)=-0.8
RDownAuto(-0.0052)=-0.005
        Points = Len(Cells(1, i)) * 1.2
        If Round(Points) >= Points Then
            Points = Round(Points)
        Else: Points = Round(Points) + 1
        End If
Function round_Up_To_Int(n As Double)
    If Math.Round(n) = n Or Math.Round(n) = 0 Then
        round_Up_To_Int = Math.Round(n)
    Else: round_Up_To_Int = Math.Round(n + 0.5)
    End If
End Function
'
' Round Up to the given number of digits
'
Function RoundUp(x As Double, digits As Integer) As Double

    If x = Round(x, digits) Then
        RoundUp = x
    Else
        RoundUp = Round(x + 0.5 / (10 ^ digits), digits)
    End If

End Function
Function RoundUpVBA(InputDbl As Double, Digits As Integer) As Double

    If InputDbl >= O Then
        If InputDbl = Round(InputDbl, Digits) Then RoundUpVBA = InputDbl Else RoundUpVBA = Round(InputDbl + 0.5 / (10 ^ Digits), Digits)
    Else
        If InputDbl = Round(InputDbl, Digits) Then RoundUpVBA = InputDbl Else RoundUpVBA = Round(InputDbl - 0.5 / (10 ^ Digits), Digits)
    End If

End Function
Function RoundUpToWhole(InputDbl As Double) As Integer

    Dim TruncatedDbl As Double

    TruncatedDbl = Fix(InputDbl)

    If TruncatedDbl <> InputDbl Then
        If TruncatedDbl >= 0 Then RoundUpToWhole = TruncatedDbl + 1 Else RoundUpToWhole = TruncatedDbl - 1
    Else
        RoundUpToWhole = TruncatedDbl
    End If

End Function