Board logo

標題: [發問] 浮點數運算處理,在VBA 程式中去呼叫WORKSHEET的ROUND [打印本頁]

作者: jj369963    時間: 2013-10-29 12:25     標題: 浮點數運算處理,在VBA 程式中去呼叫WORKSHEET的ROUND

Dear:
   之前有發問請教論壇的專業高手,得出的語法如附檔
    測試發現用vba
round(4+5)/2=4.5=4
round(4+1)/2=2.5=2

在上面2組這樣的數值搭配無法四捨五入,換成其他數值就都OK四捨五入
用worksheet的 round function
round(4+5)/2=4.5=5
round(4+1)/2=2.5=3

目想到的原因如下:
涉及浮點數運算處理,算9/2在儲存上可能是4.499999999993之類的導致vba round的結果是4,

而worksheet的 round function處理和 vba的不同,

想請問如何在VBA 程式中去呼叫WORKSHEET的ROUND,改寫 附檔的四捨五入的部份語法(讓它可以運算結果與worksheet的 round functionㄧ樣)

可參考
http://www.excelforum.com/excel-programming-vba-macros/401104-worksheet-rounding-vs-vba-rounding.html

謝謝

再麻煩各位大大指教,謝謝

[attach]16508[/attach]
作者: stillfish00    時間: 2013-10-29 14:35

回復 1# jj369963
你可以使用
Application.WorksheetFunction.Round(4.5, 0)

Application.Round(4.5, 0)
作者: Hsieh    時間: 2013-10-29 15:21

回復 1# jj369963
  1. Sub Replace_Blank()

  2. Columns("A:A").Select


  3.     Selection.Insert Shift:=xlToRight
  4.     Selection.Insert Shift:=xlToRight


  5. Range("A1").Select
  6.     ActiveCell.FormulaR1C1 = "password"

  7.     Range("B1").Select
  8.     ActiveCell.FormulaR1C1 = "user"


  9.     Dim E As Range
  10.     For Each E In Range("f:f").SpecialCells(xlCellTypeConstants)
  11.         E.Value = "'" & Replace(E, ",", "")
  12.     Next


  13. With [H:BC]
  14. .Replace "*,*", "", xlWhole '清除複選
  15. End With
  16. With [H:BC] '1~48題的欄位


  17. .Replace 1, "3@", xlWhole '將1用一個不常用符號取代


  18. .Replace 2, 1, xlWhole '將2用1取代


  19. .Replace 3, 2, xlWhole '將3用2取代


  20. .Replace "3@", 3, xlWhole '將不常用符號用3取代


  21. End With
  22. Dim A As Range, Ar(), B As Range
  23. Set Upw = CreateObject("Scripting.Dictionary") '帳密
  24. Set dic = CreateObject("Scripting.Dictionary") '參照
  25. fs = ThisWorkbook.Path & "\replace_rule.txt" 'TEXT檔案位置
  26. Close #1 '若已經開啟就先關閉
  27. With Sheets("Sheet0")
  28. Open fs For Input As #1
  29. Do Until EOF(1)
  30.    Line Input #1, mystr
  31.    If InStr(mystr, ",") > 0 Then
  32.    s = InStr(mystr, "(")
  33.    n = InStr(s, mystr, ")")
  34.    mystr = Mid(mystr, s + 1, n - s - 1)
  35.    For Each C In Split(mystr, ",")
  36.      Set A = .Rows(1).Find(C)
  37.      ReDim Preserve Ar(i)
  38.      Ar(i) = Split(A.Address, "$")(1)
  39.      i = i + 1
  40.    Next
  41.    For Each p In Ar
  42.       dic(p) = Ar '記錄公式參照欄位
  43.    Next
  44.    Erase Ar: i = 0
  45.    End If
  46. Loop
  47. Close #1
  48. With Sheets("Sheet1")
  49.   For Each A In .Range(.[A2], .[A2].End(xlDown))
  50.      Upw(CStr(A)) = Array(A.Offset(, 3).Value, A.Offset(, 2).Value) '記錄帳密
  51.   Next
  52. End With
  53. '取代複選位置
  54. Set A = .Range(.[H2], .Cells(.Rows.Count, "CQ").End(xlUp)).Find("*,*")
  55. If Not A Is Nothing Then
  56. Do
  57. ay = Split(A, ",")
  58. For i = 0 To UBound(ay)
  59. ReDim Preserve Ar(i)
  60. Ar(i) = Val(ay(i))
  61. Next
  62.   A.Value = Round(Application.Average(Ar), 0)
  63.   Erase Ar
  64.   Set A = .Range(.[H2], .Cells(.Rows.Count, "CQ").End(xlUp)).Find("*,*", A)
  65. Loop Until A Is Nothing
  66. End If
  67. i = 0
  68. .Select
  69. For Each A In .Range(.[F2], .Cells(.Rows.Count, "F").End(xlUp))
  70.    A.Offset(, -5).Resize(, 2) = Upw(CStr(A)) '填寫帳密
  71.    r = A.Row
  72.    For Each B In .Range(.Cells(r, "H"), .Cells(r, "CQ"))
  73.       If B = "" Then  '找到空格
  74.       ay = dic(Split(B.Address, "$")(1))
  75.       If Not IsEmpty(ay) Then '該儲存格有被公式引用
  76.          For i = 0 To UBound(ay)
  77.          If .Range(ay(i) & r) <> "" Then '引用的參照非空白才計入陣列
  78.            ReDim Preserve Ar(j)
  79.            Ar(j) = Range(ay(i) & r).Value
  80.            j = j + 1
  81.          End If
  82.          Next
  83.         If j > 0 Then B.Value = Application.Round(Application.WorksheetFunction.Average(Ar), 0)
  84.          Erase Ar
  85.          j = 0
  86.       End If
  87.       End If
  88.     Next
  89. Next
  90. End With

  91. Cells.Select
  92.     Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
  93.         "=(COUNTBLANK($A1:$CQ1)>0)*(COUNTA($A1:$CQ1)>0)"
  94.     Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
  95.     With Selection.FormatConditions(1).Interior
  96.         .PatternColorIndex = xlAutomatic
  97.         .ThemeColor = xlThemeColorAccent6
  98.         .TintAndShade = 0.399945066682943
  99.     End With
  100.     Selection.FormatConditions(1).StopIfTrue = True


  101. End Sub
複製代碼

作者: jj369963    時間: 2013-10-30 12:36

回復 3# Hsieh
  
     Dear Hsieh 版大 & stillfish00 大大:

    謝謝您們超迅速提供的回應,尤其是 Hsieh 版大,多次打擾真是抱歉,如有造成困擾,真是萬分抱歉,
    也謝謝您如此熱心並持續的予以回應指導,原來vba與worksheet再round指令的計算方式不一樣,再次謝謝您們平日的照顧與指導。
    也謝謝論壇的各位提供一個討論的空間。
    The VBA Round function uses 'Bankers rounding' (half the time the .5 is
rounded up, half the time down).
  The worksheet ROUND() function uses 'normal rounding' (.5 is always rounded
up)




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)