我遇到这样的情况,我在Excel中有一列带有插入的符号(翼形)的列,这些符号将具有到外部文档的超链接。我遇到的问题是,当将超链接插入到单元格中时,将应用默认的超链接格式,这会从单元格中删除带翼的字体,并将其替换为默认字体,从而导致图标被替换为数字。
我知道超链接样式可以通过单元格样式功能区进行更改,但是似乎没有一种方法可以将不同的超链接样式应用于特定范围,并且我不希望整个都使用超链接样式工作簿默认使用wingdings作为字体。
是否存在一种简单的方法,即在使用VBA插入超链接(从而覆盖默认的超链接样式)时,将字体样式应用于单元格,反之,如果删除了超链接,则应用另一种样式?
我希望未插入超链接时该单元格图标为灰色,而当插入超链接时为黑色。
任何对此事的帮助,将不胜感激。
谢谢
答案 0 :(得分:0)
此UDF将识别单元格或范围是否包含超链接对象,然后可以与条件格式一起使用:
Public Function IsHyperlink(ByVal Target As Range)
Application.Volatile
'Quick Win - look for Hyperlink Object
IsHyperlink = (Target.Hyperlinks.Count > 0)
'Check if Excel can convert Target to a Hyperlink
'Important because the worksheet recalculates BEFORE that happens
If Not IsHyperlink Then
IsHyperlink = (Application.WorksheetFunction.CountIf(Target, "www.*") + Application.WorksheetFunction.CountIf(Target, "ftp://*") _
+ Application.WorksheetFunction.CountIf(Target, "http://*") + Application.WorksheetFunction.CountIf(Target, "https://*")) > 0
End If
'Check for HYPERLINK() formula
If (Target.HasFormula Or IsNull(Target.HasFormula)) And Not IsHyperlink Then
Dim rCell As Range, sFormula As String, aFormula() As String, lStart As Long, lEnd As Long
For Each rCell In Target.Cells
If rCell.HasFormula Then
sFormula = rCell.Formula
'Remove any text
While Len(sFormula) > Len(Replace(sFormula, Chr(34), "")) + 1
lStart = InStr(sFormula, Chr(34))
lEnd = InStr(lStart + 1, sFormula, Chr(34))
sFormula = Left(sFormula, lStart - 1) & Mid(sFormula, lEnd + 1)
Wend
'Replace "+", "-", "*", "/", "%"
sFormula = Replace(Replace(Replace(Replace(Replace(sFormula, "+", ","), "-", ","), "*", ","), "/", ","), "%", ",")
'Replace "=", "(", ")", " "
sFormula = Replace(Replace(Replace(Replace(sFormula, "=", ","), "(", ","), ")", ","), " ", ",")
'Search for "HYPERLINK"
If InStr(UCase(sFormula & ","), ",HYPERLINK,") Then
IsHyperlink = True
Exit For
End If
End If
Next rCell
End If
End Function
当前,它无法检测到通过 现在可以了! HYPERLINK
公式嵌入的超链接(例如=HYPERLINK("https://stackoverflow.com/", "Visit Stack Overflow!")
)
根据事件顺序,Excel将在之前重新计算UDF,它将将键入单元格中的所有URL转换为超链接。因此,我在其中添加了一行内容,该行还将找到Excel 可以转换的单元格-否则,您将需要进行额外的计算才能获得结果