简体   繁体   English

自动填充事件更改 VBA

[英]AutoFill with Event Change VBA

I wrote this code below, but it's working just when I write something in Columns D and E and just doing the autofill for column C.我在下面写了这段代码,但是当我在 D 列和 E 列中写一些东西并且只是为 C 列进行自动填充时,它才起作用。 The idea is that whenever I write something in any cell in a line higher than 4, the columns are autofilled.这个想法是,每当我在高于 4 行的任何单元格中写入内容时,这些列都会自动填充。 Does anyone know why it's not working and how could I solve it?有谁知道为什么它不起作用,我该如何解决?

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

Try this.尝试这个。 I could not reproduce the issue you're experiencing, but this should do what you expect我无法重现您遇到的问题,但这应该符合您的预期

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM