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

问题描述 投票:0回答:2

我基本上尝试将一个超链接放到Sheet1中的单元格,如果文本匹配“92/65 / EEC”,则将其链接到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
excel vba loops if-statement hyperlink
2个回答
0
投票

应该

Anchor:=cell

现在它对我有用。


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
© www.soinside.com 2019 - 2024. All rights reserved.