Excel在运行VBA宏时删除查询表连接

问题描述 投票:2回答:1

我正面临着一个从未有过的有趣问题,似乎无法找到有关在线的任何信息。这是设置:

我正在遍历表中的一组行-每行都有一列用于工作表名称和一个链接,我想从中链接一些数据。我已经编写了VBA代码,该代码循环遍历每一行,使用正确的名称创建一个新工作表,在该工作表中创建一个querytable,Web刮除正确的链接并删除该查询表。

这里是代码:

Sub WQ_Refresh(wsname As String, wqName As String, wqURL As String, strFC As String)

Dim ws As Worksheet
Dim wq As QueryTable
Dim errno As Long
Dim loopcnt As Integer
Dim refreshTime As Double
Dim lastrow As Long

refreshTime = Timer

Application.StatusBar = "Now downloading " & wqName & " for " & strFC

If wsname = "" Then Exit Sub

Set ws = ThisWorkbook.Sheets(wsname)

    If ws.QueryTables.Count > 0 Then

    Set wq = ws.QueryTables(1)

    wq.Delete

End If

lastrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

If lastrow > 1 Then lastrow = lastrow + 1

Set wq = ws.QueryTables.Add(Connection:="URL;" & wqURL, Destination:=ws.Range("A" & lastrow))

errno = 1
loopcnt = 0

Do While Not (errno = 0 And loopcnt < 10)

    On Error Resume Next

    With wq

        .FieldNames = True
        .WebFormatting = xlWebFormattingNone
        .WebSelectionType = xlAllTables
        .Refresh BackgroundQuery:=False
        .Delete

    End With

    loopcnt = loopcnt + 1
    errno = Err.Number

    If loopcnt = 10 Then HashtagFail wqName

    On Error GoTo 0

Loop

If lastrow > 1 Then

   ws.Rows(lastrow - 1 & ":" & lastrow).Delete

End If

Set wq = Nothing

Application.StatusBar = "Downloaded " & wqName & " in " & Round(Timer - refreshTime, 0) & " seconds"

由于某种原因,我无法理解,wq.Delete函数将正确删除创建的wq,但也会删除另一个工作表中的连接,该工作表是文件的一部分。

要清除-它不会删除连接。它仍然存在,但是如果您查看其属性,然后转到“使用位置”选项卡,它将显示为不再在任何工作表中使用。

我不知道为什么会这样-对我来说,代码应该清楚地删除循环中工作表中的连接,而不会影响文件中的任何其他连接。

这似乎是一个基本的excel错误,但我真的很希望您有任何输入,因为我完全被卡住了。

感谢您的帮助!

excel vba
1个回答
0
投票

基于注释形式@DeanDeVilliers。有效的解决方案是按名称引用表。如果有人好奇,这里是工作代码

Sub WQ_Refresh(wsname As String, wqName As String, wqURL As String, strFC As String)
Dim ws As Worksheet
Dim wq As QueryTable
Dim errno As Long
Dim loopcnt As Integer
Dim refreshTime As Double
Dim lastrow As Long

refreshTime = Timer

Application.StatusBar = "Now downloading " & wqName & " for " & strFC

If wsname = "" Then Exit Sub

Set ws = ThisWorkbook.Sheets(wsname)

    If ws.QueryTables.Count > 0 Then ws.QueryTables("WQ_" & wqName).Delete





lastrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

If lastrow > 1 Then lastrow = lastrow + 1

Set wq = ws.QueryTables.Add(Connection:="URL;" & wqURL, Destination:=ws.Range("A" & lastrow))

errno = 1
loopcnt = 0

Do While Not (errno = 0 And loopcnt < 10)

    On Error Resume Next

    With wq

        .FieldNames = True
        .WebFormatting = xlWebFormattingNone
        .WebSelectionType = xlAllTables
        .Refresh BackgroundQuery:=False
        .Name = "WQ_" & wqName
        ' .Delete

    End With

    loopcnt = loopcnt + 1
    errno = Err.Number

    If loopcnt = 10 Then HashtagFail wqName

    On Error GoTo 0

ws.QueryTables("WQ_" & wqName).Delete

Loop

If lastrow > 1 Then

   ws.Rows(lastrow - 1 & ":" & lastrow).Delete

End If

Set wq = Nothing

Application.StatusBar = "Downloaded " & wqName & " in " & Round(Timer - refreshTime, 0) & " seconds"

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.