将新行添加到 Table 并插入范围内的数据

Add new Row to Table and insert data from range

有人要求我将电子表格中的数据更改为 table。

数据需要从另一个工作簿粘贴到此table。

当数据为电子表格形式时,我的代码工作正常,但在更改为 table 后,我无法获取添加新行或将数据粘贴到该行的代码。

这是当前代码,只要数据不是 Table 形式,它就可以工作。

Sub CopytoResults()

Dim destSht As Worksheet

Application.ScreenUpdating = False

'Copy the range 
ActiveSheet.Range("C52:AJ52").Select
Selection.Copy

'Open the Results spreadsheet and paste to the next available row
Workbooks.Open (ActiveWorkbook.Path & "\Quality_ResultsTST.xlsx")
Set destSht = ActiveWorkbook.Worksheets("Staff Results")

destSht.Activate

'Paste Data
destSht.Cells(Rows.Count, 1).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues

'Save and Close the Results Spreadsheet
destSht.Parent.Close True

'Set Focus to the QA_QC Assessment form
Application.CutCopyMode = False
Windows("QA Form V2.xlsm").Activate
ActiveSheet.Range("E7").Select

Application.ScreenUpdating = True
    
End Sub

Table 名称是结果

我已经尝试了来自该网站和其他网站的多种解决方案,但均无济于事。

我知道使用 select 等是错误的形式,但这不是我的代码,我正在更新旧应用程序。

非常感谢

使用 Table 会是这样的:

Sub CopytoResults()
    
    Dim rngSrc as range, wb as workbook
    
    Application.ScreenUpdating = False
    
    'Copy the range 
    Set rngSrc = ActiveSheet.Range("C52:AJ52")
    
    set wb = Workbooks.Open(ActiveWorkbook.Path & "\Quality_ResultsTST.xlsx") 'ThisWorkbook?
    
    'add a new row to the table and assign the data to its Range
    with wb.sheets("Staff Results").listobjects("Results").listrows.add.range
        .value = rngSrc.value 
    end with

    wb.close true
    thisworkbook.Activate
    ActiveSheet.Range("E7").Select
    
End Sub