使用字符串函数更改单元格中的超链接文本

时间:2016-10-21 20:28:11

标签: excel excel-vba vba

给定一个包含超链接的单元格,我想使用字符串函数(如Mid)更改显示的文本,但仍保留超链接地址和功能。我能做什么?感谢。

示例:

示例1:

细胞包含这个超级大片:15 Mary Street

必需的输出是这个超链接:Mary Stree - 如果我点击“Mary Street”就像我点击“15 Mary Street”时的链接一样。

示例2:

enter image description here

2 个答案:

答案 0 :(得分:1)

我不确定这是否会保留您的链接或打破它。如果它打破了它,请告诉我。

Sub test()
     removeLeadingNumbers Range("A1:A10")
End Sub


Sub removeLeadingNumbers(myRng As Range)
Dim cel As Range, chr As String
Dim i As Long, k As Long

For Each cel In myRng
    If Not IsEmpty(cel) Then
    i = Len(cel)
    For i = 1 To Len(cel)
        If IsNumeric(cel.Characters(i, 1).Text) Then
            k = k + 1
        Else
            Exit For
        End If
    Next i
    Debug.Print "remove the first " & k & " letters"
    cel.Value = Trim(Right(cel.Value, Len(cel.Value) - k))
    End If
Next cel
End Sub

答案 1 :(得分:0)

如果要更改显示的文本以删除前导数字后跟空格,请尝试:

Sub DumpTheNumbers()
    Dim h As Hyperlink, txt As String

    For Each h In ActiveSheet.Hyperlinks
        txt = h.TextToDisplay
        If InStr(txt, " ") <> 0 Then
            arr = Split(txt, " ")
            If IsNumeric(arr(0)) Then
                arr(0) = " "
                h.TextToDisplay = Trim(Join(arr, " "))
            End If
        End If
    Next h
End Sub