VBA Adding a Table Name

Mike_VBA

Board Regular
Joined
Dec 27, 2008
Messages
70
Hi All,

This is my code:

Code:
With Sheets("XYZ").ListObjects.Add(SourceType:=0, Source:=Array( _
        "OLEDB;Provider=SQLOLEDB.1;Password=XYZ@1;Persist Security Info=True;User ID=XYZ;Data Source=XYZ;Use Procedure for Prepare=1;" _
        , _
        "Auto Translate=True;Packet Size=4096;Workstation ID=MKUMAR72;Use Encryption for Data=False;Tag with column collation when possib" _
        , "le=False;TableStyleName=TableStyleMedium8;Initial Catalog=ecompass"), Destination:=Sheets("XYZ").Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM XYZ WHERE M='XYZ' AND Months = '" & month & "' ORDER BY Name")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = True
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .SourceConnectionFile = _
        "snapa.odc"
        .Refresh BackgroundQuery:=True
        End With

For this, excel keeps adding a random table name like :

Table_ExternalData_152
Table_ExternalData_153
Table_ExternalData_154

Is there a way I can name it myself ? Thanks!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try using a construction like

Code:
Set MyTable=Sheets("XYZ").ListObjects.Add(SourceType:=0, Source:=Array( _
        "OLEDB;Provider=SQLOLEDB.1;Password=XYZ@1;Persist Security Info=True;User ID=XYZ;Data Source=XYZ;Use Procedure for Prepare=1;" _
        , _
        "Auto Translate=True;Packet Size=4096;Workstation ID=MKUMAR72;Use Encryption for Data=False;Tag with column collation when possib" _
        , "le=False;TableStyleName=TableStyleMedium8;Initial Catalog=ecompass"), Destination:=Sheets("XYZ").Range("$A$1")).QueryTable
 
With MyTable
     .Name="MyNameForThisTable"
     .CommandType.......................................
 
Upvote 0
Thanks a lot Yard. The one that worked out for me is :

Using Listobjects.Listobject.name = "XYZwhatever"

Also using Listobjects.Listobject.name = "Tablestylelight8" helps out for themes that Excel has bundled in..


Try using a construction like

Code:
Set MyTable=Sheets("XYZ").ListObjects.Add(SourceType:=0, Source:=Array( _
        "OLEDB;Provider=SQLOLEDB.1;Password=XYZ@1;Persist Security Info=True;User ID=XYZ;Data Source=XYZ;Use Procedure for Prepare=1;" _
        , _
        "Auto Translate=True;Packet Size=4096;Workstation ID=MKUMAR72;Use Encryption for Data=False;Tag with column collation when possib" _
        , "le=False;TableStyleName=TableStyleMedium8;Initial Catalog=ecompass"), Destination:=Sheets("XYZ").Range("$A$1")).QueryTable
 
With MyTable
     .Name="MyNameForThisTable"
     .CommandType.......................................
 
Upvote 0

Forum statistics

Threads
1,203,101
Messages
6,053,535
Members
444,670
Latest member
laurenmjones1111

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