[英]excel vba - Macro to copy filtered data on cell change
I'm trying to write a macro to do the following: 我正在尝试编写一个宏来执行以下操作:
I tried writing this: 我试着这样写:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("A:A")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
MsgBox "Cell " & Target.Address & " has changed. New value is: " _
& Target.Value
copy_filter (Target)
End If
End Sub
Sub copy_filter(Changed)
Worksheets("Sheet2").Select
With Worksheets("Sheet2")
With .Range("$A$1:$L$5943")
.AutoFilter Field:=1, Criteria1:=Changed.Value
.SpecialCells(xlCellTypeVisible).Select
Selection.Offset(1, 0).Copy
End With
End With
Worksheets("Sheet1").Select
Worksheets("Sheet1").Range(Changed.Address).Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
The first Private Sub
watches for change events on Sheet1, I put it into the sheet module, not the Workbook module, and it works, since the box gets shown correctly, however on the second Sub
I get an error on this line: .AutoFilter Field:=1, Criteria1:=Changed.Value
, the error says: Run-time error '424': Object required
. 第一个
Private Sub
监视Sheet1上的更改事件,我将其放入工作表模块中,而不是工作簿模块中,并且可以正常工作,因为该框正确显示,但是在第二个Sub
我在此行出现错误: .AutoFilter Field:=1, Criteria1:=Changed.Value
,错误显示: Run-time error '424': Object required
。
I can't figure out what I'm doing wrong. 我不知道我在做什么错。
Change copy_filter (Target)
to either 将
copy_filter (Target)
更改为
call copy_filter (Target)
OR 要么
copy_filter Target
Read this: https://msdn.microsoft.com/en-us/library/office/gg251432.aspx 阅读此: https : //msdn.microsoft.com/en-us/library/office/gg251432.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.