【问题标题】:Increment decimal place by 0.05 in Word VBA在 Word VBA 中将小数位增加 0.05
【发布时间】:2015-11-03 05:57:56
【问题描述】:

我首先要说我大约一周前刚开始自学 VBA,所以我可能问的问题不正确,但是...

我正在尝试在 Word VBA 中编写一个循环,该循环将增加一个从书签中检索到的文本部分计算的数字。我希望它四舍五入到最接近的 0.05,因此 .87 变为 .90,而 .21 变为 .25。

我写的模块如下:

A = ActiveDocument.Bookmarks("SRebateIncome").Range.Text
B = ActiveDocument.Bookmarks("RebateDefault").Range.Text
C = ((A - 6000) * 0.15)
D = B - C
E = B + D
F = (18200 + ((445 + E) / 0.19)) + 1
G = (0.19 * 18200) + 445 + E + (37000 * (0.015 + 0.325 - 0.19))
H = (G / (0.015 + 0.325)) + 1
I = ActiveDocument.Bookmarks("TRebateIncome").Range.Text

If F < 37000 = True Then
    J = (0.125 * (I - F))
Else
    J = (0.125 * (I - H))
End If

K = E - J
K = Format(Round(K, 2), "###,##0.00")
'round K up to the nearest .00 or .05
If K <> "###,###.#0" = False or K <> "###,###.#5") = False Then
    Do
        K = K + 0.01
    Loop Until K = "###,###.#0" = True or K <> "###,###.#5") = True
End If

Set RebateOutput = ActiveDocument.Bookmarks("RebateOutput").Range
RebateOutput.Text = K

现在假设书签“SRebateIncome”、“RebateDefault”和“TRebateIncome”的输入值分别为 10175、1602 和 43046,我预计输出为 1460.80,但“K”返回为 1460.78。

在这个阶段,我对在 word 中使用 Excel 一无所知(除了将电子表格复制/粘贴到文档中,我不想这样做)。

任何帮助将不胜感激

谢谢!

【问题讨论】:

  • 乘以 20,四舍五入到最接近的整数,然后除以 20。

标签: vba ms-word


【解决方案1】:

您可以使用 excel 对象和天花板函数来完成此操作

Option Explicit

Sub RoundText()

    Dim dblSRebateIncome As Double
    Dim dblRebateDefault As Double
    Dim dblTRebateIncome As Double
    Dim dblFinal As Double
    Dim rngOutput As Range
    Dim oExcel As Object

    ' Load the variables
    Set oExcel = CreateObject("Excel.Application")
    Set rngOutput = ActiveDocument.Bookmarks("RebateOutput").Range
    dblSRebateIncome = CDbl(ActiveDocument.Bookmarks("SRebateIncome").Range.Text)
    dblRebateDefault = CDbl(ActiveDocument.Bookmarks("RebateDefault").Range.Text)
    dblSRebateIncome = CDbl(ActiveDocument.Bookmarks("TRebateIncome").Range.Text)

    dblFinal = GetCalculatedValue(dblSRebateIncome, dblRebateDefault, dblTRebateIncome)

    dblFinal = oExcel.worksheetfunction.Ceiling(dblFinal, 0.05)

    rngOutput.Text = Format$(dblFinal, "###,##0.00")

End Sub

Function GetCalculatedValue(ByVal dblSIncome As Double, _
                            ByVal dblDefault As Double, _
                            ByVal dblTIncome) As Double

    ' Declare all the intermediate variables.
    Dim c As Double, d As Double, e As Double
    Dim f As Double, g As Double, h As Double
    Dim j As Double, ret As Double

    ' Perform the complicated calculation
    c = ((dblSIncome - 6000) * 0.15)
    d = dblDefault - c
    e = dblDefault + d
    f = (18200 + ((445 + e) / 0.19)) + 1
    g = (0.19 * 18200) + 445 + e + (37000 * (0.015 + 0.325 - 0.19))
    h = (g / (0.015 + 0.325)) + 1

    If f < 37000 Then
        j = (0.125 * (dblTIncome - f))
    Else
        j = (0.125 * (dblTIncome - h))
    End If

    ret = e - j


    ' Return the value of the fucntion
    GetCalculatedValue = ret

End Function

希望这会有所帮助。 :)

【讨论】:

    【解决方案2】:
    Dim x As Double
    x = 1.111 'E.g.
    Debug.Print Round(x * 20, 0)/20 '>> 1.10
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      相关资源
      最近更新 更多