Changing queryTable name in 2007/2010 using VBA

Lxocram

New Member
Joined
Mar 2, 2012
Messages
1
Since Excel 2007 querytables are part of a Listobject.

I've been able to succesfully alter the CommandText and Connection property using VBA

For Each ws In ActiveWorkbook.Sheets
For Each lo In ws.ListObjects
If lo.SourceType = XlListObjectSourceType.xlSrcQuery Then
lo.QueryTable.PreserveColumnInfo = False
lo.QueryTable.PreserveFormatting = False
lo.QueryTable.Refresh BackgroundQuery:=False
lo.QueryTable.RefreshStyle = XlCellInsertionMode.xlInsertDeleteCells
lo.QueryTable.Commandtext = Replace(lo.QueryTable.Commandtext,OldCommandText,NewCommandText)
lo.QueryTable.Connection=Replace(lo.QueryTable.Connection,OldConnectionText,NewConnectionText)


but when I try to alter the QueryTable.Name property I get an error
I can't even print the name
Debug.Print lo.QueryTable.Name does not work

I can print and change the Listobject name "External_Data_..." but I want to change the Connection name and description

http://office.microsoft.com/en-us/excel-help/connection-properties-HA010175443.aspx

I can do it via

Dim lngConn As Long
For lngConn = .Connections.Count To 1 Step -1
.Connections(lngConn).Name = Replace(.Connections(lngConn).Name, OldConnectionName, NewConnectionName)
Next lngConn
End With

But how do i match the activeworkbook.connection to the listobject.queryTable.Connection
 

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.
Will this work for you?

Dim sh As Worksheet, LO As ListObject, QT As QueryTable

Set sh = ActiveSheet
Set QT = sh.ListObjects.Item(1).QueryTable

MsgBox QT.WorkbookConnection.name
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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