|
类似这样的,你把逻辑完善就可以了
Private Sub Worksheet_Change(ByVal Target As Range)
Dim BbE As Double
Dim i As Integer
Dim j As Integer
Dim EventsBAK
Dim CalculBAK
If ((Target.Cells.Column = 2 Or Target.Cells.Column = 5) And Cells(Target.Cells.Row, 1) <> "" And Cells(Target.Cells.Row, 5) <> "") Then '修改单位制体系
EventsBAK = Application.EnableEvents
CalculBAK = Application.Calculation
Application.EnableEvents = False
Application.Calculation = xlManual
BbE = Cells(Target.Cells.Row, 2) / Cells(Target.Cells.Row, 5)
For i = Target.Cells.Row + 1 To 40
If (Cells(i, 1) <> "" Or Cells(i, 5) = "") Then
Exit For
End If
If (BbE > 0) Then
Cells(i, 6) = BbE * Cells(i, 5)
Else
Cells(i, 6) = “”
End If
Next i
Application.Calculation = CalculBAK
Application.EnableEvents = EventsBAK
ElseIf (Target.Cells.Column = 5 And Cells(Target.Cells.Row, 1) = "" And Cells(Target.Cells.Row, 5) <> "") Then
EventsBAK = Application.EnableEvents
CalculBAK = Application.Calculation
Application.EnableEvents = False
Application.Calculation = xlManual
BbE = 0
For i = 1 To 40
If (Cells(Target.Cells.Row - i, 1) <> "") Then
BbE = Cells(Target.Cells.Row - i, 2) / Cells(Target.Cells.Row - i, 5)
Exit For
End If
Next i
If (BbE > 0) Then
Cells(Target.Cells.Row, 6) = BbE * Cells(Target.Cells.Row, 5)
Else
Cells(Target.Cells.Row, 6) = ""
End If
Application.Calculation = CalculBAK
Application.EnableEvents = EventsBAK
End If
End Sub |
|