每次更改行时运行Excel宏

5tmbdcev  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(121)

我有下面的Excel宏,现在它运行每次有任何变化的工作表.我想它只运行行已更改,因为工作表是大到足以滞后一两秒,每次运行.
这是可能的吗?也是可能的运行在任何工作表中的任何行在工作簿中,如果该行被更改?
模块1:

  1. Function ValidNumber(NUM1 As Integer, NUM2 As Integer) As Boolean
  2. If (IsNumeric(Cells(NUM1, NUM2).Value) And Not IsEmpty(Cells(NUM1, NUM2).Value)) Then
  3. ValidNumber = True
  4. Else
  5. ValidNumber = False
  6. End If
  7. End Function
  8. Sub Calculate1(NUM As Integer)
  9. Dim EntryPrice As Double
  10. Dim ExitPrice As Double
  11. Dim Invested As Double
  12. Dim PL As Double
  13. Dim Margin As Double
  14. Dim InvestPercentage As Double
  15. If (Cells(NUM, 6).Value = "L" And ValidNumber(NUM, 7) And ValidNumber(NUM, 8) And ValidNumber(NUM, 10)) Then
  16. EntryPrice = Cells(NUM, 7)
  17. ExitPrice = Cells(NUM, 8)
  18. Invested = Cells(NUM, 10)
  19. InvestPercentage = (Invested / EntryPrice) * 100
  20. PL = ((ExitPrice / 100) * InvestPercentage) - Invested
  21. Margin = PL / Invested
  22. If (Cells(NUM, 8) < Cells(NUM, 7)) Then
  23. Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
  24. Cells(NUM, 8).Interior.ColorIndex = 3
  25. ElseIf (Cells(NUM, 8) > Cells(NUM, 7) And Cells(NUM, 8) < Cells(NUM, 9)) Then
  26. Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
  27. Cells(NUM, 8).Interior.Color = RGB(255, 140, 0)
  28. Else
  29. Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
  30. Cells(NUM, 8).Interior.ColorIndex = 10
  31. End If
  32. If (PL > 0) Then
  33. Cells(NUM, 11).Font.Color = RGB(255, 255, 255)
  34. Cells(NUM, 11).Interior.ColorIndex = 10
  35. Cells(NUM, 12).Font.Color = RGB(255, 255, 255)
  36. Cells(NUM, 12).Interior.ColorIndex = 10
  37. Else
  38. Cells(NUM, 11).Font.Color = RGB(255, 255, 255)
  39. Cells(NUM, 11).Interior.ColorIndex = 3
  40. Cells(NUM, 12).Font.Color = RGB(255, 255, 255)
  41. Cells(NUM, 12).Interior.ColorIndex = 3
  42. End If
  43. Cells(NUM, 11).Value = PL
  44. Cells(NUM, 12).Value = Margin
  45. ElseIf (Cells(NUM, 6).Value = "S" And ValidNumber(NUM, 7) And ValidNumber(NUM, 8) And ValidNumber(NUM, 10)) Then
  46. EntryPrice = Cells(NUM, 7)
  47. ExitPrice = Cells(NUM, 8)
  48. Invested = Cells(NUM, 10)
  49. InvestPercentage = (Invested / EntryPrice) * 100
  50. PL = ((ExitPrice / 100) * InvestPercentage) - Invested
  51. Margin = PL / Invested
  52. If (Cells(NUM, 8) > Cells(NUM, 7)) Then
  53. Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
  54. Cells(NUM, 8).Interior.ColorIndex = 3
  55. ElseIf (Cells(NUM, 8) < Cells(NUM, 7) And Cells(NUM, 8) > Cells(NUM, 9)) Then
  56. Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
  57. Cells(NUM, 8).Interior.Color = RGB(255, 140, 0)
  58. Else
  59. Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
  60. Cells(NUM, 8).Interior.ColorIndex = 10
  61. End If
  62. If (PL < 0) Then
  63. Cells(NUM, 11).Font.Color = RGB(255, 255, 255)
  64. Cells(NUM, 11).Interior.ColorIndex = 10
  65. Cells(NUM, 12).Font.Color = RGB(255, 255, 255)
  66. Cells(NUM, 12).Interior.ColorIndex = 10
  67. PL = Abs(PL)
  68. Margin = Abs(Margin)
  69. Else
  70. Cells(NUM, 11).Font.Color = RGB(255, 255, 255)
  71. Cells(NUM, 11).Interior.ColorIndex = 3
  72. Cells(NUM, 12).Font.Color = RGB(255, 255, 255)
  73. Cells(NUM, 12).Interior.ColorIndex = 3
  74. PL = -Abs(PL)
  75. Margin = -Abs(Margin)
  76. End If
  77. Cells(NUM, 11).Value = PL
  78. Cells(NUM, 12).Value = Margin
  79. Else
  80. Cells(NUM, 8).Interior.Color = xlNone
  81. Cells(NUM, 11).Interior.Color = xlNone
  82. Cells(NUM, 12).Interior.Color = xlNone
  83. Cells(NUM, 8).Font.Color = RGB(0, 0, 0)
  84. Cells(NUM, 11).Font.Color = RGB(0, 0, 0)
  85. Cells(NUM, 12).Font.Color = RGB(0, 0, 0)
  86. Cells(NUM, 11).Clear
  87. Cells(NUM, 12).Clear
  88. End If
  89. Range("B:B").NumberFormat = "mm/dd/yy"
  90. Range("K:K").NumberFormat = "$#0.00"
  91. Range("L:L").NumberFormat = "#0.00000%"
  92. End Sub
  93. Sub CalculatePL()
  94. For i = 3 To Sheet1.UsedRange.Rows.Count + 1
  95. Calculate1 (i)
  96. Next
  97. End Sub

字符串
表1:

  1. Private Sub Worksheet_Activate()
  2. Application.EnableEvents = False
  3. For i = 3 To Sheet1.UsedRange.Rows.Count + 1
  4. Calculate1 (i)
  5. Next
  6. Application.EnableEvents = True
  7. End Sub
  8. Private Sub Worksheet_Change(ByVal Target As Range)
  9. Application.EnableEvents = False
  10. For i = 3 To Sheet1.UsedRange.Rows.Count + 1
  11. Calculate1 (i)
  12. Next
  13. Application.EnableEvents = True
  14. End Sub

mo49yndu

mo49yndu1#

删除Sub Worksheet_Activate()并修改Sub Worksheet_Change,如下所示。

  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Application.EnableEvents = False
  3. Calculate1 (Target.Row)
  4. Application.EnableEvents = True
  5. End Sub

字符串

相关问题