如何检查Excel VBA中是否存在查询表?

时间:2018-01-22 17:45:27

标签: excel vba excel-vba

我想检查QueryTables(1)是否存在,如果存在,则删除它。我已经知道用什么代码来删除它:

If Not ActiveSheet.QueryTables(1) Is Nothing Then ...

但我不确定如何首先检查它是否存在,以便在尝试删除不存在的查询表时不会出现错误。

更新: 我尝试使用

import graphviz as gv
import pylab

g1 = gv.Graph(format='png')

g1.node('A')
g1.node('B')
g1.edge('A', 'B')

g1.view()
print(g1.source) 

filename = g1.render(filename='img/g1')

pylab.savefig('filename.png')

得到运行时错误'9':下标超出范围。

当我使用我正在搜索的查询表的实际名称(称为“MonitorData”)时,我得到同样的错误。

2 个答案:

答案 0 :(得分:4)

您也可以查看计数

If Activesheet.QueryTables.Count > 0 then
    Activesheet.QueryTables(1).Delete
End If

答案 1 :(得分:1)

您可以随时关闭错误,运行它然后重新打开它们:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
    <shape>
        <solid android:color="@android:color/transparent" />
        <corners android:radius="3dp" />
    </shape>
</item>
<item>
    <shape>
        <solid android:color="#f36774" />            
        <size
            android:height="100dp"
            android:width="100dp" />
        <corners
            android:radius="3dp" />
    </shape>
</item>
<item>
    <shape>
        <corners android:radius="3dp" />
        <solid android:color="@android:color/transparent" />
        <stroke
            android:width="2dp"
            android:color="#f36774" />
    </shape>
</item>