我有下面的Excel宏,现在它运行每次有任何变化的工作表.我想它只运行行已更改,因为工作表是大到足以滞后一两秒,每次运行.
这是可能的吗?也是可能的运行在任何工作表中的任何行在工作簿中,如果该行被更改?
模块1:
Function ValidNumber(NUM1 As Integer, NUM2 As Integer) As Boolean
If (IsNumeric(Cells(NUM1, NUM2).Value) And Not IsEmpty(Cells(NUM1, NUM2).Value)) Then
ValidNumber = True
Else
ValidNumber = False
End If
End Function
Sub Calculate1(NUM As Integer)
Dim EntryPrice As Double
Dim ExitPrice As Double
Dim Invested As Double
Dim PL As Double
Dim Margin As Double
Dim InvestPercentage As Double
If (Cells(NUM, 6).Value = "L" And ValidNumber(NUM, 7) And ValidNumber(NUM, 8) And ValidNumber(NUM, 10)) Then
EntryPrice = Cells(NUM, 7)
ExitPrice = Cells(NUM, 8)
Invested = Cells(NUM, 10)
InvestPercentage = (Invested / EntryPrice) * 100
PL = ((ExitPrice / 100) * InvestPercentage) - Invested
Margin = PL / Invested
If (Cells(NUM, 8) < Cells(NUM, 7)) Then
Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
Cells(NUM, 8).Interior.ColorIndex = 3
ElseIf (Cells(NUM, 8) > Cells(NUM, 7) And Cells(NUM, 8) < Cells(NUM, 9)) Then
Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
Cells(NUM, 8).Interior.Color = RGB(255, 140, 0)
Else
Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
Cells(NUM, 8).Interior.ColorIndex = 10
End If
If (PL > 0) Then
Cells(NUM, 11).Font.Color = RGB(255, 255, 255)
Cells(NUM, 11).Interior.ColorIndex = 10
Cells(NUM, 12).Font.Color = RGB(255, 255, 255)
Cells(NUM, 12).Interior.ColorIndex = 10
Else
Cells(NUM, 11).Font.Color = RGB(255, 255, 255)
Cells(NUM, 11).Interior.ColorIndex = 3
Cells(NUM, 12).Font.Color = RGB(255, 255, 255)
Cells(NUM, 12).Interior.ColorIndex = 3
End If
Cells(NUM, 11).Value = PL
Cells(NUM, 12).Value = Margin
ElseIf (Cells(NUM, 6).Value = "S" And ValidNumber(NUM, 7) And ValidNumber(NUM, 8) And ValidNumber(NUM, 10)) Then
EntryPrice = Cells(NUM, 7)
ExitPrice = Cells(NUM, 8)
Invested = Cells(NUM, 10)
InvestPercentage = (Invested / EntryPrice) * 100
PL = ((ExitPrice / 100) * InvestPercentage) - Invested
Margin = PL / Invested
If (Cells(NUM, 8) > Cells(NUM, 7)) Then
Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
Cells(NUM, 8).Interior.ColorIndex = 3
ElseIf (Cells(NUM, 8) < Cells(NUM, 7) And Cells(NUM, 8) > Cells(NUM, 9)) Then
Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
Cells(NUM, 8).Interior.Color = RGB(255, 140, 0)
Else
Cells(NUM, 8).Font.Color = RGB(255, 255, 255)
Cells(NUM, 8).Interior.ColorIndex = 10
End If
If (PL < 0) Then
Cells(NUM, 11).Font.Color = RGB(255, 255, 255)
Cells(NUM, 11).Interior.ColorIndex = 10
Cells(NUM, 12).Font.Color = RGB(255, 255, 255)
Cells(NUM, 12).Interior.ColorIndex = 10
PL = Abs(PL)
Margin = Abs(Margin)
Else
Cells(NUM, 11).Font.Color = RGB(255, 255, 255)
Cells(NUM, 11).Interior.ColorIndex = 3
Cells(NUM, 12).Font.Color = RGB(255, 255, 255)
Cells(NUM, 12).Interior.ColorIndex = 3
PL = -Abs(PL)
Margin = -Abs(Margin)
End If
Cells(NUM, 11).Value = PL
Cells(NUM, 12).Value = Margin
Else
Cells(NUM, 8).Interior.Color = xlNone
Cells(NUM, 11).Interior.Color = xlNone
Cells(NUM, 12).Interior.Color = xlNone
Cells(NUM, 8).Font.Color = RGB(0, 0, 0)
Cells(NUM, 11).Font.Color = RGB(0, 0, 0)
Cells(NUM, 12).Font.Color = RGB(0, 0, 0)
Cells(NUM, 11).Clear
Cells(NUM, 12).Clear
End If
Range("B:B").NumberFormat = "mm/dd/yy"
Range("K:K").NumberFormat = "$#0.00"
Range("L:L").NumberFormat = "#0.00000%"
End Sub
Sub CalculatePL()
For i = 3 To Sheet1.UsedRange.Rows.Count + 1
Calculate1 (i)
Next
End Sub
字符串
表1:
Private Sub Worksheet_Activate()
Application.EnableEvents = False
For i = 3 To Sheet1.UsedRange.Rows.Count + 1
Calculate1 (i)
Next
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
For i = 3 To Sheet1.UsedRange.Rows.Count + 1
Calculate1 (i)
Next
Application.EnableEvents = True
End Sub
型
1条答案
按热度按时间mo49yndu1#
删除
Sub Worksheet_Activate()
并修改Sub Worksheet_Change
,如下所示。字符串