【问题标题】:EXCEL-VBA hyperlinks conversion inquiryEXCEL-VBA超链接转换查询
【发布时间】:2015-04-17 11:14:19
【问题描述】:
Dim RITMRow As Long
Dim ws1 As Worksheet
Dim RITMstorage As String
Dim LastRow As Long




Set ws1 = Sheets("Tracker")


LastRow = ws1.Range("A" & Rows.Count).End(xlUp).Row

For RITMRow = 2 To LastRow


RITMstorage = ws1.Range("A" & RITMRow).Value



ws1.Range("A" & RITMRow).Hyperlinks.Add Anchor:=ws1.Range("A" & RITMRow), _
    Address:="https://site.site.com/sc_req_item.do?sys_id=" & RITMstorage, _
    ScreenTip:="Request Number", _
    TextToDisplay:=RITMstorage


Next RITMRow


With ws1

.Cells.Font.Size = "8"
.Cells.RowHeight = 11.25
.Cells.Font.Name = "Calibri"
.Range("A1").EntireRow.RowHeight = 25

End With

嗨,我上面的代码用于将列转换为超链接。正如您所看到的,它的效率相当低,因为每次我单击按钮时,它都会返回并将所有内容再次转换为超链接,即使是那些已经是超链接的。请指出我正确的方向。我需要一种方法来检测已经有超链接的列,偏移量为 1,然后转换非超链接单元格。

提前致谢。

【问题讨论】:

    标签: vba excel hyperlink


    【解决方案1】:

    尝试从单元格中获取地址并检查是否出现错误:

    Dim url As String
    Dim isLink As Boolean
    For RITMRow = 2 To LastRow
    
        On Error Resume Next
        url = ws1.Range("A" & RITMRow).Hyperlinks(1).SubAddress
        isLink = (Err.Number = 0)
        On Error GoTo 0
    
        If Not isLink Then
            RITMstorage = ws1.Range("A" & RITMRow).Value
            ws1.Range("A" & RITMRow).Hyperlinks.Add Anchor:=ws1.Range("A" & RITMRow), _
                Address:="https://site.site.com/sc_req_item.do?sys_id=" & RITMstorage, _
                ScreenTip:="Request Number", _
                TextToDisplay:=RITMstorage
        End If
    
    Next RITMRow
    

    【讨论】:

    • 嘿!有用!我只是想问一下 hyperlinks(1).subaddress 是做什么的?谢谢!
    • 我试图获取单元格中第一个超链接的子地址。如果没有,则会出错。我出于习惯使用它 - Hyperlinks.Count 可能在大多数情况下也可以工作(假设您正在使用已知的工作表)。
    • @user2519726 ...我只记得原因。见this post
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    相关资源
    最近更新 更多