A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
The error "subscript out of range" in VBA usually occurs when the code is trying to reference an index that does not exist or is outside the defined range. Based on your code, there are a few possible reasons for the error.
To fix this, you can replace the line: ActiveSheet.ListObjects("Table_ExternalData_1"). Name = "Table_Table_0"
with:
ActiveSheet.ListObjects(1). Name = "Table_Table_0"
Also, you can replace the line: Range("M2"). Select with: ActiveSheet.Range("M2"). Select
This ensures that the cell is selected in the active sheet.
Please try this update code:
Sub Test() ' ' Macro2 Macro ' ' ActiveSheet.Range("M2"). Select Selection.Copy ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _ "let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/SPYD/history""))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Date"", type date}, {""Open"", type text}, {""High"", type text}, {""Low"", type text}, {""Close*"", type text}, {""Adj Close**"", type text}, {""Volume"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type""" ActiveSheet.ListObjects(1). Name = "Table_Table_0" ActiveWorkbook.Worksheets.Add With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _ "OLEDB; Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$; Location=""Table 0""; Extended Properties=""""" _ , Destination:=Range("$A$1")). QueryTable . CommandType = xlCmdSql . CommandText = Array("SELECT * FROM [Table 0]") . RowNumbers = False . FillAdjacentFormulas = False . PreserveFormatting = True . RefreshOnFileOpen = False . BackgroundQuery = True . RefreshStyle = xlInsertDeleteCells . SavePassword = False . SaveData = True . AdjustColumnWidth = True . RefreshPeriod = 0 . PreserveColumnInfo = True . ListObject.DisplayName = "Table_Table_0" . Refresh BackgroundQuery:=False End With ActiveSheet.Range("F2"). Select Application.CutCopyMode = False ActiveCell.FormulaR1C1 = "38.17" Sheets("Holdings"). Select Range("J2"). Select
Best Regards, Shakiru