Error using ListObject.Add to create a Table Style

﹥>﹥吖頭↗ 提交于 2019-12-05 15:16:13

Based on your code, you cannot add a ListObject to the Worksheet with an underlying QueryTable still in place. If you try to do this in normal Excel (non-COM), you will get an error like:

If you hit Yes there and record a macro while it does its work, Excel just deletes the QueryTable and adds the ListObject. Deleting the QueryTable does not affect the underlying data.

In the VBA world, your code would look like this:

Sub DeleteQueryTableAndAddListObject()

    Dim sht As Worksheet
    Set sht = ActiveSheet

    ''code up here to create a QueryTable

    Dim i As Integer
    For i = sht.QueryTables.Count To 1 Step -1
        sht.QueryTables(i).Delete
    Next i

    sht.ListObjects.Add xlSrcRange, sht.UsedRange, , xlYes

End Sub

Taking a stab at PowerShell (not my native tongue) you should be able to do:

$worksheet.QueryTables.item($Connector.name).Delete()

or possibly:

$Connector.Delete()

since $Connector appears to be a valid reference to the QueryTable object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!