【问题标题】:AutoFill with Event Change VBA使用事件更改 VBA 自动填充
【发布时间】:2020-12-04 11:19:25
【问题描述】:

我在下面写了这段代码,但是当我在 D 列和 E 列中写一些东西并且只是为 C 列做自动填充时它才起作用。这个想法是,每当我在高于 4 行的任何单元格中写一些东西时,列是自动填充的。有谁知道它为什么不起作用,我该如何解决?

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

Application.ScreenUpdating = False


If Target.Row >= 4 Then


Planilha3.Activate


ul = Cells(Rows.Count, "D").End(xlUp).Row


With Range("C4")

.FormulaR1C1 = "=RC[1]&RC[2]"

.AutoFill Destination:=Range("C4:C" & ul)

End With

With Range("B4")

    .FormulaR1C1 = "=IF(RC[1]<>"""",COUNTIF(R4C3:RC[1],RC[1]),"""")"
    
    .AutoFill Destination:=Range("B4:B" & ul)

End With

 
 With Range("A4")
 
 .FormulaR1C1 = "=RC[2]&RC[1]"
 .AutoFill Destination:=Range("A4:A" & ul)

End With

With Range("J4")

    .FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-7],'Lista de Fornecedores'!C1:C4,2,FALSE),""Forn não cadastrado"")"
    .AutoFill Destination:=Range("J4:J" & ul)

End With

With Range("K4")

    .FormulaR1C1 = "=IF(RC[-1]=""Forn não cadastrado"",""Forn não cadastrado"", ""Recebido"")"
    .AutoFill Destination:=Range("K4:K" & ul)
  
End With


Application.EnableEvents = True


Application.ScreenUpdating = True



End If

End Sub

【问题讨论】:

  • 我想了解为什么当我在 D203 中写一些东西时,什么都没有发生。但是如果我在 D203 和 E203 中写了一些东西,那么自动填充就会发生在 C 列中。
  • 您可能想要做的第一件事是在sub 的末尾加上Application.EnableEvents = TrueApplication.ScreenUpdating = True If 语句。 (或在If 语句的开头将它们设置为False)。
  • Target 可能是一个多单元格范围,Target.Row 将返回最小行号。这是您期望的行为吗?
  • 更改上述代码后,您的代码运行良好。顺便说一句,您不需要自动填充;您可以简单地将公式设置为多单元格范围,所有内容都会适当更新,例如Range("C4:C" &amp; ul).FormulaR1C1 = "=RC[1]&amp;RC[2]"
  • 嘿!谢谢!!实际上,每当我更改从第 4 行开始的行中的任何单元格时,我都希望运行此代码。

标签: excel vba autofill


【解决方案1】:

试试这个。我无法重现您遇到的问题,但这应该符合您的预期

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Cleanup

    Application.EnableEvents = False
    Application.ScreenUpdating = False

    Dim ul As Long
    
    If Target.Row >= 4 Then
        '* Assuming Planilha3 is the current sheet in question, you don't need to activate it
        'Planilha3.Activate
            
        ul = Me.Cells(Rows.Count, "D").End(xlUp).Row
        With Me.Range("A4:K" & ul)
            '* No need to autofill, you can set formulae to whole column at once
            .Columns("C").Formula = "=D4&E4"
            .Columns("B").Formula = "=IF(C4<>"""",COUNTIF($C$4:C4,C4),"""")"
            .Columns("A").Formula = "=C4&B4"
            .Columns("J").Formula = "=IFERROR(VLOOKUP(C4,'Lista de Fornecedores'!$A:$D,2,FALSE),""Forn não cadastrado"")"
            .Columns("K").Formula = "=IF(J4=""Forn não cadastrado"",""Forn não cadastrado"", ""Recebido"")"
        End With
    End If

Cleanup:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

【讨论】:

  • 请注意,当代码正确格式化和缩进时,代码看起来会好得多!
  • 嘿,谢谢!很高兴知道我不需要自动填充!当我在一个模块中尝试时,你的代码运行了,从 de ul 行开始!但是,它不能作为事件工作。每当我在下面的单元格中写入大于 4 的内容时,代码就不会运行。
  • 这意味着您的活动已被禁用。您可以重新启动excel或单独运行Application.EnableEvents = True
  • 我赞同@SJR 的观点。格式良好的代码使其更具可读性,因此有助于减少错误,因为人们可以看到所有代码片段是如何组合在一起的。
  • 嘿!我意识到我的 Application.EnableEvents 是 False,这就是它不起作用的原因!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多