直接输入或公式

时间:2017-03-15 20:32:20

标签: excel excel-vba vba

我有一个电子表格,我希望最终用户能够输入值,或者电子表格会自动填充公式。如果直接输入被删除,我还希望它重新填充公式。目前以下代码什么都不做。任何帮助将不胜感激!

Private Sub Worksheet_Change1(ByVal target As Range)

If target.Cells.Count > 1 Then Exit Sub

If Not Intersect(target, Range("B8")) Is Nothing Then

Range("B8").Formula = "=IF($B$1=""DC"",IF(MAX('TY Data'!$L:$L)>=VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),SUMIFS('TY Data'!$K:$K,'TY Data'!$A:$A,5101200,'TY Data'!$L:$L,VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),'TY Data'!$D:$D,""AP0345R"",'TY Data'!$I:$I,'Wage Run'!$D$1),0),IF($B$1=""Division"",IF(MAX('TY Data'!$L:$L)>=VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),SUMIFS('TY Data'!$K:$K,'TY Data'!$A:$A,5101200,'TY Data'!$L:$L,VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),'TY Data'!$D:$D,""AP0345R"",'TY Data'!$M:$M,'Wage Run'!$D$1),0)" _
& ",IF($B$1=""GBU"",IF(MAX('TY Data'!$L:$L)&>=VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),SUMIFS('TY Data'!$K:$K,'TY Data'!$A:$A,5101200,'TY Data'!$L:$L,VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),'TY Data'!$D:$D,""AP0345R"",'TY Data'!$N:$N,'Wage Run'!$D$1),0),IF($B$1=""Rollup"",IF(MAX('TY Data'!$L:$L)>=VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),SUMIFS('TY Data'!$K:$K,'TY Data'!$A:$A,5101200,'TY Data'!$L:$L,VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),'TY Data'!$D:$D,""AP0345R"",'TY Data'!$O:$O,'Wage Run'!$D$1),0),""""))))"

End If
End Sub

编辑:

将新公式添加到旧子

后,新公式如下所示
Private Sub Worksheet_Change(ByVal target As Range)

If target.Cells.Count > 1 Then Exit Sub

If Not Intersect(target, Range("B1")) Is Nothing Then
    Range("C1").ClearContents
End If

If target.Cells.Count > 1 Then Exit Sub

If Not Intersect(target, Range("B8")) Is Nothing And target.Value = "" Then

Range("B8").Formula = "=IF($B$1=""DC"",IF(MAX('TY Data'!$L:$L)>=VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),SUMIFS('TY Data'!$K:$K,'TY Data'!$A:$A,5101200,'TY Data'!$L:$L,VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),'TY Data'!$D:$D,""AP0345R"",'TY Data'!$I:$I,'Wage Run'!$D$1),0),IF($B$1=""Division"",IF(MAX('TY Data'!$L:$L)>=VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),SUMIFS('TY Data'!$K:$K,'TY Data'!$A:$A,5101200,'TY Data'!$L:$L,VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),'TY Data'!$D:$D,""AP0345R"",'TY Data'!$M:$M,'Wage Run'!$D$1),0)" _
& ",IF($B$1=""GBU"",IF(MAX('TY Data'!$L:$L)&>=VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),SUMIFS('TY Data'!$K:$K,'TY Data'!$A:$A,5101200,'TY Data'!$L:$L,VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),'TY Data'!$D:$D,""AP0345R"",'TY Data'!$N:$N,'Wage Run'!$D$1),0),IF($B$1=""Rollup"",IF(MAX('TY Data'!$L:$L)>=VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),SUMIFS('TY Data'!$K:$K,'TY Data'!$A:$A,5101200,'TY Data'!$L:$L,VLOOKUP('Wage Run'!$F$1,Dates!$A:$D,3,0),'TY Data'!$D:$D,""AP0345R"",'TY Data'!$O:$O,'Wage Run'!$D$1),0),""""))))"

End If

End Sub

1 个答案:

答案 0 :(得分:0)

您没有禁用事件触发器。当您将公式写入B8时,Worksheet_Change将在其自身上运行。如果公式恰好返回"",那么它将继续在其自身上运行直到您崩溃。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    On Error GoTo Safe_Exit
    Application.EnableEvents = False

    Select Case Target.Address(0, 0)
        Case "B1"
            Target.Offset(0, 1).ClearContents
        Case "B8"
            If Not Target.HasFormula Then _
                Target.Formula = "=IFERROR(IF(MAX('TY Data'!$L:$L)>=VLOOKUP('Wage Run'!$F$1, Dates!$A:$D, 3, FALSE), " & _
                                          "SUMIFS('TY Data'!$K:$K, " & _
                                                 "'TY Data'!$A:$A, 5101200, " & _
                                                 "'TY Data'!$L:$L, VLOOKUP('Wage Run'!$F$1, Dates!$A:$D, 3, FALSE), " & _
                                                 "'TY Data'!$D:$D, ""AP0345R""," & _
                                                 "INDEX('TY Data'!$I:$O, 0, LOOKUP($B$1, {""DC"",""Division"",""GBU"",""Rollup""}, {1,5,6,7})), 'Wage Run'!$D$1), 0), """")"
        Case Else
            'do nothing
    End Select

Safe_Exit:
    Application.EnableEvents = True

End Sub

在相关主题上,这是我对您的公式的修改。在B8中,

=IFERROR(IF(MAX('TY Data'!$L:$L)>=VLOOKUP('Wage Run'!$F$1, Dates!$A:$D, 3, FALSE),
      SUMIFS('TY Data'!$K:$K, 'TY Data'!$A:$A, 5101200,
                              'TY Data'!$L:$L, VLOOKUP('Wage Run'!$F$1, Dates!$A:$D, 3, FALSE),
                              'TY Data'!$D:$D, "AP0345R",
                              INDEX('TY Data'!$I:$O, 0, LOOKUP($B$1, {"DC","Division","GBU","Rollup"}, {1,5,6,7})), 'Wage Run'!$D$1), 0), "")

INDEX的最后一行是OR(B1={"DC","Division","GBU","Rollup"})发挥作用的地方。