Create Table VBA Not Working

shellp

Board Regular
Joined
Jul 7, 2010
Messages
199
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
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.

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
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Untested but maybe this:

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")
With ws
lrow = .Cells(.Rows.count, "A").End(xlUp).Row
lcol = .Cells(1, .Columns.count).End(xlToLeft).Column
.ListObjects.Add(xlSrcRange, .Range(.Cells(1, 1), .Cells(lrow, lcol)), , xlYes).Name = "Chart_List"
End With
End Sub
 
Upvote 0
I figured out another way to do this. I already reference number of rows of data for another purpose on another worksheet so I referenced it here and used the range value.

Code:
Sub make_table()
Dim ws1 As Worksheet
Dim ws2 as worksheet
Dim r_long As range
 
 
Set ws1 = ThisWorkbook.Sheets("Chart_List")
Set ws2=ThisWorkbook.Sheets("Export")

R_Long=ws2.range("C8:C8")
 
ws.ListObjects.Add(xlSrcRange, ws1.range("A$1:$R$"& R_Long), , xlYes).Name = "Chart_List"
 
 End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top