0

我已经使用 Microsoft Forms 制作了一个表单,并且在同一个 Excel 文件中,我有一个宏,它会在每次发生更改时触发,例如。A2-A20。

所以我的目标是每次有人提交并且数据进入时触发宏。但是当有人提交表单并且数据被添加到 Excel 文件时,宏似乎不会被触发。
当我自己在其中一个单元格中写一些东西时,宏工作正常。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    
    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("A2:A20")
 
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then
        
        'Get name for new workbook
        Dim WBnew As Variant
        WBnew = Range("E" & Target.Row).Value
        
        ' Creates a reference to workbook object
        Dim WB As Workbook
 
        'Adding a New Workbook
        Set WB = Workbooks.Add
    
        'Set where to save the Workbook and name
        ActiveWorkbook.SaveAs Filename:="C:\Users\" & WBnew & ".xls"
 
         'Copy entire row
        Workbooks("Test forms.xlsm").Worksheets("Form1").Range(Target.Row & ":" & Target.Row).Copy
 
        'Paste to row 2 in the new workbook
        Workbooks(WBnew & ".xls").Worksheets("Sheet1").Range("1:1").Insert
        
        'Copy row 1 in old workbook and paste it to the new workbooks row 1
        Workbooks("Test forms.xlsm").Worksheets("Form1").Range("1:1").Copy
        Workbooks(WBnew & ".xls").Worksheets("Sheet1").Range("1:1").Insert

        'Save the workbook
        ActiveWorkbook.Save
 
End If
 
End Sub

知道该怎么做,所以当新数据从表单进入时,宏会被触发?

提前致谢!

4

0 回答 0