Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加新表时Excel VBA自动运行宏_Excel_Vba - Fatal编程技术网

添加新表时Excel VBA自动运行宏

添加新表时Excel VBA自动运行宏,excel,vba,Excel,Vba,我有一个Macro1,它复制了表1并粘贴到另一张表中的表2 Sub Macro1() Dim ws1 As Worksheet, ws2 As Worksheet Dim Source As ListObject, destination As ListObject Dim lRows As Long, lColumns As Long Set ws1 = Sheets("Sheet1") Set ws2 = Sheets("Sheet2") Set S

我有一个Macro1,它复制了表1并粘贴到另一张表中的表2

Sub Macro1()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Source As ListObject, destination As ListObject
Dim lRows As Long, lColumns As Long

Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set Source = ws1.ListObjects("Table1")
Set destination = ws2.ListObjects("Table2")
lRows = Source.ListRows.Count
lColumns = Source.ListColumns.Count

'clearing contents of table 2
If Not destination.DataBodyRange Is Nothing Then destination.DataBodyRange.Delete

'copy data from table 1
    Source.DataBodyRange.Copy

'paste data into table 2 
    ws2.Activate
    destination.InsertRowRange.Resize(lRows, lColumns).Activate
    ActiveSheet.Paste
End Sub
现在我需要在表1中粘贴新数据时随时自动运行这个宏1 我试试这个:

Private Sub Macro2 (ByVal Target As Range)
    If Target.ListObjects("Table1") Then
        Call Macro1
    End If
End Sub

如何在粘贴新表时连接两个宏以自动运行Macro1???

这将为您指明正确的方向: