ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] ¯BÂI¼Æ¹Bºâ³B²z¡A¦bVBA µ{¦¡¤¤¥h©I¥sWORKSHEETªºROUND

[µo°Ý] ¯BÂI¼Æ¹Bºâ³B²z¡A¦bVBA µ{¦¡¤¤¥h©I¥sWORKSHEETªºROUND

Dear:
   ¤§«e¦³µo°Ý½Ð±Ð½×¾Âªº±M·~°ª¤â¡A±o¥Xªº»yªk¦pªþÀÉ
    ´ú¸Õµo²{¥Îvba
round(4+5)/2=4.5=4
round(4+1)/2=2.5=2

¦b¤W­±2²Õ³o¼Ëªº¼Æ­È·f°tµLªk¥|±Ë¤­¤J¡A´«¦¨¨ä¥L¼Æ­È´N³£OK¥|±Ë¤­¤J
¥Îworksheetªº round function
round(4+5)/2=4.5=5
round(4+1)/2=2.5=3

¥Ø·Q¨ìªº­ì¦]¦p¤U¡G
¯A¤Î¯BÂI¼Æ¹Bºâ³B²z¡Aºâ9/2¦bÀx¦s¤W¥i¯à¬O4.499999999993¤§Ãþªº¾É­Pvba roundªºµ²ªG¬O4¡A

¦Óworksheetªº round function³B²z©M vbaªº¤£¦P¡A

·Q½Ð°Ý¦p¦ó¦bVBA µ{¦¡¤¤¥h©I¥sWORKSHEETªºROUND¡A§ï¼g ªþÀɪº¥|±Ë¤­¤Jªº³¡¥÷»yªk(Åý¥¦¥i¥H¹Bºâµ²ªG»Pworksheetªº round function£¸¼Ë)

¥i°Ñ¦Ò
http://www.excelforum.com/excel-programming-vba-macros/401104-worksheet-rounding-vs-vba-rounding.html

ÁÂÁÂ

¦A³Â·Ð¦U¦ì¤j¤j«ü±Ð¡AÁÂÁÂ

round error_2.rar (110.39 KB)

¦^´_ 1# jj369963
§A¥i¥H¨Ï¥Î
Application.WorksheetFunction.Round(4.5, 0)
©Î
Application.Round(4.5, 0)

TOP

¦^´_ 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 '²M°£½Æ¿ï
  15. End With
  16. With [H:BC] '1~48ÃDªºÄæ¦ì


  17. .Replace 1, "3@", xlWhole '±N1¥Î¤@­Ó¤£±`¥Î²Å¸¹¨ú¥N


  18. .Replace 2, 1, xlWhole '±N2¥Î1¨ú¥N


  19. .Replace 3, 2, xlWhole '±N3¥Î2¨ú¥N


  20. .Replace "3@", 3, xlWhole '±N¤£±`¥Î²Å¸¹¥Î3¨ú¥N


  21. End With
  22. Dim A As Range, Ar(), B As Range
  23. Set Upw = CreateObject("Scripting.Dictionary") '±b±K
  24. Set dic = CreateObject("Scripting.Dictionary") '°Ñ·Ó
  25. fs = ThisWorkbook.Path & "\replace_rule.txt" 'TEXTÀɮצì¸m
  26. Close #1 '­Y¤w¸g¶}±Ò´N¥ýÃö³¬
  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 '°O¿ý¤½¦¡°Ñ·ÓÄæ¦ì
  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) '°O¿ý±b±K
  51.   Next
  52. End With
  53. '¨ú¥N½Æ¿ï¦ì¸m
  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)) '¶ñ¼g±b±K
  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 '¸ÓÀx¦s®æ¦³³Q¤½¦¡¤Þ¥Î
  76.          For i = 0 To UBound(ay)
  77.          If .Range(ay(i) & r) <> "" Then '¤Þ¥Îªº°Ñ·Ó«DªÅ¥Õ¤~­p¤J°}¦C
  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
½Æ»s¥N½X
¾Ç®üµL²P_¤£®¢¤U°Ý

TOP

¦^´_ 3# Hsieh
  
     Dear Hsieh ª©¤j & stillfish00 ¤j¤j¡G

    ÁÂÁ±z­Ì¶W¨³³t´£¨Ñªº¦^À³¡A¤×¨ä¬O Hsieh ª©¤j¡A¦h¦¸¥´ÂZ¯u¬O©êºp¡A¦p¦³³y¦¨§xÂZ¡A¯u¬O¸U¤À©êºp¡A
    ¤]ÁÂÁ±z¦p¦¹¼ö¤ß¨Ã«ùÄòªº¤©¥H¦^À³«ü¾É¡A­ì¨Óvba»Pworksheet¦Around«ü¥Oªº­pºâ¤è¦¡¤£¤@¼Ë¡A¦A¦¸ÁÂÁ±z­Ì¥­¤éªº·ÓÅU»P«ü¾É¡C
    ¤]ÁÂÁ½׾ªº¦U¦ì´£¨Ñ¤@­Ó°Q½×ªºªÅ¶¡¡C
    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)

TOP

        ÀR«ä¦Û¦b : §g¤l¦p¤ô¡AÀH¤è´N¶ê¡AµL³B¤£¦Û¦b¡C
ªð¦^¦Cªí ¤W¤@¥DÃD