简体   繁体   English

VBA Excel 表范围和超链接

[英]VBA Excel table range and hyperlinks

The execution crash on this line at the end of my sub after I added some hyperlinks in an other column ( Method 'Range' of object'_Global' failed ).在我在另一列中添加一些超链接后,在我的 sub 末尾的这一行上执行崩溃( Method 'Range' of object'_Global' failed )。

Range("TABLE_XXX[[#All],[COLUMN1]]")

When I go in debug and press F8 it works.当我在调试中 go 并按 F8 时它可以工作。 If I delete the code that puts hyperlinks it works like a charm.如果我删除放置超链接的代码,它就像一个魅力。

Here is the code to add the hyperlinks这是添加超链接的代码

With mySheet
    .Hyperlinks.Add Anchor:=.Cells(depRow, urlCol), _
    Address:="https://www.google.com/", _
    ScreenTip:="Google", _
    TextToDisplay:="https://www.google.com/"
End With

Any idea of what happens?知道会发生什么吗?

EDIT: I just wrote a sample that reproduce perfectly the bug.编辑:我刚刚编写了一个完美重现该错误的示例。

Sub Macro3()
    Dim objTable As ListObject
    Dim rng As Range
    ActiveSheet.Cells.Clear
    Set objTable = ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:B2"), , xlYes)
    objTable.Name = "TABLE_XXX"
    objTable.ListColumns(1).Name = "col1"
    objTable.ListColumns(2).Name = "col2"

    Set rng = Range("TABLE_XXX[[#All],[col1]]")

    With ActiveSheet
        .Hyperlinks.Add Anchor:=.Cells(2, 2), _
        Address:="https://www.google.com/", _
        ScreenTip:="Google", _
        TextToDisplay:="https://www.google.com/"
    End With

    Set rng = Range("TABLE_XXX[[#All],[col2]]")
End Sub

The first Set rng works but the second one don't (except in debug).第一个Set rng有效,但第二个无效(调试除外)。

Replace代替

Set rng = Range("TABLE_XXX[[#All],[col2]]")

with

Set rng = ActiveSheet.ListObjects("TABLE_XXX").ListColumns("col2").Range

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM