Excel VBA - Select Active Table in Active Sheet in Active Workbook?

MEUserII

Board Regular
Joined
Oct 27, 2017
Messages
91
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
I am trying to write a Macro that would select the Active Table in the Active Sheet in the Active Workbook for data stored in "C3:H9".
I know the following VBA references can be used: "Application.ActiveWorkbook.ActiveSheet.ActiveCell" can be used to refer to the ActiveCell as mentioned in Link 1, and "ActiveSheet.ActiveCell.CurrentRegion.Select" can be used to refer to the rectangular block of cells surrounded by one or more blank rows or columns as mentioned in Link 2, and "ActiveCell.ListObject.Name" can be used to refer to the active table as mentioned in Link 3.
My question then is: why does the below code not work for referring to the Active Table in the Active Sheet in the Active Workbook by using Application.ActiveWorkbook.ActiveSheet.ActiveCell.ListObject.Name?

Link 1: https://www.experts-exchange.com/questions/27513085/Format-Excel-From-MS-Access-VBA.html
Link 2: https://bettersolutions.com/excel/cells-ranges/vba-selecting-cells.htm
Link 3: https://stackoverflow.com/questions/37215665/referencing-active-table-in-vba

Sub Macro1()

Range("C3:H9").Select

Dim Table1 As ListObject
Set Table1 = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)

Range("C3").Select

Application.ActiveWorkbook.ActiveSheet.ActiveCell.ListObject.Name.TableStyle = "TableStyleLight1"

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Not exactly sure what your wanting to do.
Try this if your trying to set the Table Style.
Not sure why you want to refer to
Range("C3:H9").Select

This script will set the style you want to the Table in the active cell range

Code:
Sub Get_Table_Name()
ans = ActiveCell.ListObject.Name
ActiveSheet.ListObjects(ans).TableStyle = "TableStyleLight1"
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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