使用If条件循环插入超链接

时间:2018-01-03 10:53:59

标签: excel vba loops if-statement hyperlink

如果文本匹配" 92/65 / EEC"我基本上尝试将超链接放到Sheet1中的单元格,将其链接到Sheet2。我不明白我在哪里错了。 你能帮忙吗?

Sub Legislation()

 ' Define
 Dim lrow As Long, rng As Range, cell As Range
 lrow = Cells(Cells.Rows.Count, "J").End(xlUp).Row

' Set the range where to apply the code
Set rng = Range("J5:J" & lrow)

' Define what to look for
 For Each cell In rng
 If InStr(1, cell.Value, "92/65/EEC", vbTextCompare) > 0 Then
 With cell.Validation
  ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
    "'List of EU legislation '!A8", TextToDisplay:="92/65/EEC"
    End With
 End If

Next cell

End Sub

2 个答案:

答案 0 :(得分:0)

应该是

Anchor:=cell

现在它对我有用。

答案 1 :(得分:0)

请试一试......

For Each cell In rng
    If InStr(1, cell.Value, "92/65/EEC", vbTextCompare) > 0 Then
      ActiveSheet.Hyperlinks.Add Anchor:=cell, Address:="", SubAddress:= _
        "'List of EU legislation '!A8", TextToDisplay:="92/65/EEC"
    End If
Next cell