我有以下VBA脚本:
Sub ConvertToHyperlinks()
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
Application.ActiveSheet.Hyperlinks.Add Rng, "http://" & Rng.Value
Next
End Sub
它会将所选单元格转换为可以单击的超链接。 例如,如果单元格的值为" example.com" ,则会转到" http://example.com"
它运行正常,唯一的问题是,如果单元格为空,它仍会将其转换为值为" http://" 的超链接
如何让它忽略具有空值的单元格,而不是将" http://" 添加到它们?
答案 0 :(得分:1)
这样的事情(你的WorkRng
也需要1行)
Sub ConvertToHyperlinks()
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.InputBox("Range", xTitleId, Selection.Address, Type:=8)
For Each Rng In WorkRng
If Len(Rng.Value) > 0 Then ActiveSheet.Hyperlinks.Add Rng, "http://" & Rng.Value
Next
End Sub