我正在尝试编写一个宏来执行以下操作:
我试着写这个:
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
第一个Private Sub
监视Sheet1上的更改事件,我将它放入工作表模块,而不是工作簿模块,并且它工作正常,因为框正确显示,但是在第二个Sub
我在这一行收到错误:.AutoFilter Field:=1, Criteria1:=Changed.Value
,错误上写着:Run-time error '424': Object required
。
我无法弄清楚我做错了什么。
答案 0 :(得分:1)
将copy_filter (Target)
更改为
call copy_filter (Target)
OR
copy_filter Target
阅读本文:https://msdn.microsoft.com/en-us/library/office/gg251432.aspx