Attempting to copy data from a data table to a different workbook - why am I erroring?
09:20 07 Jul 2026

I have a script I'm working on to transfer two tables worth of output into a concatenation sheet, however it is erroring on the noted line as "subscript out of range" - I have triple checked and there is no issue with leading or trailing spaces in the names, so I am trying to work out if there is a code error leading to it not looking for the data table in the right place.

Sub ExportToAnalysis()  

Dim InputBook As Workbook  
Dim AnalysisBook As Workbook  
Dim InputMachines As Worksheet  
Dim InputProtocols As Worksheet  
Dim AnalysisMachines As Worksheet  
Dim AnalysisProtocols As Worksheet  
  
Set InputBook = ThisWorkbook  
Set InputMachines = InputBook.Worksheets("Machine Data")  
Set InputProtocols = InputBook.Worksheets("Template Data")  
Set AnalysisBook = Workbooks.Open("\[filepath\]")  
Set AnalysisMachines = AnalysisBook.Worksheets("Machine Data")  
Set AnalysisProtocols = AnalysisBook.Worksheets("Protocol Data")  
  
InputBook.Activate  
InputMachines.ListObjects("MachineDataTable").DataBodyRange.Select 'this line errors as subscript out of range  
Selection.Copy  
AnalysisBook.Activate  
AnalysisMachines.Columns("A:A").Select  
Selection.SpecialCells(xlCellTypeBlanks).Select  
Selection.Paste  
  
InputBook.Activate  
InputTemplates.ListObjects("TemplateDataTable").DataBodyRange.Select  
Selection.Copy  
AnalysisBook.Activate  
AnalysisProtocols.Columns("A:A").Select  
Selection.SpecialCells(xlCellTypeBlanks).Select  
Selection.Paste  
  
AnalysisBook.Save  
Application.CutCopyMode = False  
AnalysisBook.Close  
  
End Sub
excel vba