Hello
I am trying to create a table via VBA. It works when I am on the worksheet that I want the table to be on, but this worksheet will be hidden so I need it to work by allowing me to identify the worksheet. I cannot figure out why I am getting a run-time error "the worksheet range for the table data must be on the same sheet as the table being created". Below code works when I am on the page of "chart_list" but I not if I am on another worksheet.
I am trying to create a table via VBA. It works when I am on the worksheet that I want the table to be on, but this worksheet will be hidden so I need it to work by allowing me to identify the worksheet. I cannot figure out why I am getting a run-time error "the worksheet range for the table data must be on the same sheet as the table being created". Below code works when I am on the page of "chart_list" but I not if I am on another worksheet.
Code:
Sub make_table()
Dim ws As Worksheet
Dim lrow As Long
Dim lcol As Long
Dim rng As range
Set ws = ThisWorkbook.Sheets("Chart_List")
lrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
lcol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
ws.ListObjects.Add(xlSrcRange, range(Cells(1, 1), Cells(lrow, lcol)), , xlYes).Name = "Chart_List"
End Sub