具有不同范围的私有子工作表事件

时间:2017-04-05 14:11:10

标签: excel-vba vba excel

我知道您只能有一个私有子工作表更改事件,但我正在努力为我需要的事件组合代码。我是VBA的新手,所以我们非常感谢任何帮助或建议。使用精选案例会更有效吗?

首先需要的代码:

Private Sub Worksheet_Change(ByVal target As Range)

Dim cell As Range
Set cell = Range("AK9:AR50")

Application.EnableEvents = False
If Not Application.Intersect(cell, target) Is Nothing Then
If target.Column = 37 Then
    target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
ElseIf target.Column = 38 Then
    target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
End If
If target.Column = 39 Then
    target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
ElseIf target.Column = 40 Then
    target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
End If
If target.Column = 41 Then
    target.Offset(, 1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
ElseIf target.Column = 42 Then
    target.Offset(, -1).Value = target.Value / Range("V" & target.Row).Value
End If
If target.Column = 43 Then
    target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
ElseIf target.Column = 44 Then
    target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
End If
End If
Application.EnableEvents = True

End Sub

需要第二个代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim controlRng, nRng As Range
Set controlRng = Range("AF9:AF1000")
Set nRng = Intersect(controlRng, Target)

If nRng Is Nothing Then Exit Sub

If Target.Value = "No Promotion" Then
    Target.Offset(0, 1) = Range("M" & Target.Row).Value
ElseIf Target.Value = "Promotion" Then
    Target.Offset(0, 1) = ""
ElseIf Target.Value = "Demotion" Then
    Target.Offset(0, 1) = ""
ElseIf Target.Value = "Partner" Then
    Target.Offset(0, 1) = ""
ElseIf Target.Value = "" Then
    Target.Offset(0, 1) = ""
End If
End Sub

1 个答案:

答案 0 :(得分:0)

Select Case肯定会整理你的代码。您可能还想构建一个目标不超过单个单元格的检查。

where