在链接被点击后,在相邻单元格中写入”true”。

huangapple go评论78阅读模式
英文:

Write "true" in the adjacent cell after the link is clicked

问题

以下是代码部分的中文翻译:

项目的目标是设置一个VBA代码,一旦您点击链接,它就会在相邻的单元格中写入“true”,如果用户尚未点击链接,则写入“false”。

我从一个小代码开始,只是为了知道如何继续:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = Range("A1").Address Then
        Range("B1").Value = "True"
    End If
End Sub

这段代码检查点击的超链接是否在单元格 A1 中,如果是的话,就在单元格 B1 中写入“True”。
但是,这个宏仅在链接直接写在单元格 A1 中时才起作用,而我想要:

  1. VBA代码可以处理由超链接函数创建的链接。
  2. 只要用户没有点击链接,宏必须在相邻单元格中显示“false”,否则显示“true”。
  3. 此宏必须能够应用于包含超文本链接的所有单元格,无论它们是如何创建的。
  4. hyperlink() 中的 friendly_name 必须与链接的地址不同。

为了解决这个问题,我做了以下修改:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Dim rng As Range
    Dim matchFound As Boolean
    
    For Each rng In Me.UsedRange
        If rng.Hyperlinks.Count > 0 Then
            If rng.Hyperlinks(1).Address = Target.Address Or rng.Hyperlinks(1).TextToDisplay = Target.TextToDisplay Then
                rng.Offset(0, 1).Value = "True"
                matchFound = True
            Else
                If rng.Offset(0, 1).Value <> "True" Then
                    rng.Offset(0, 1).Value = "False"
                End If
            End If
        End If
    Next rng
    
    If Not matchFound Then
        MsgBox "未找到匹配的超链接。"
    End If
End Sub

但是,当点击使用 HYPERLINK() 函数创建的超链接时,这个宏不起作用,按理说,如果用户没有点击链接,宏应该显示“false”,但实际上不起作用。

你能帮助我吗?

英文:

Well, the goal of the project is to set up a VBA code that writes "true" in the adjacent cell as soon as you click on a link and false if the user has not yet clicked on the link.

I started with a small code, just to know how I was going to proceed:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = Range(&quot;A1&quot;).Address Then
        Range(&quot;B1&quot;).Value = &quot;True&quot;
    End If
End Sub

This code checks to see if the clicked hyperlink is in cell A1, and if it is, it writes "True" in cell B1.
But the macro only works if the link is written directly in cell A1, and I wanted:

  1. VBA code works with a link created by the hyperlink function.
  2. As long as the user has not clicked on the link, the macro must display "false" in the adjacent cell and "true" otherwise.
  3. This macro must be able to be applied to all cells containing hypertext links regardless of how they were created.
  4. friendly_name in the hyperlink() has to be different from the address of the link.

To fix that, I did this:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Dim rng As Range
    Dim matchFound As Boolean
    
    For Each rng In Me.UsedRange
        If rng.Hyperlinks.Count &gt; 0 Then
            If rng.Hyperlinks(1).Address = Target.Address Or rng.Hyperlinks(1).TextToDisplay = Target.TextToDisplay Then
                rng.Offset(0, 1).Value = &quot;True&quot;
                matchFound = True
            Else
                If rng.Offset(0, 1).Value &lt;&gt; &quot;True&quot; Then
                    rng.Offset(0, 1).Value = &quot;False&quot;
                End If
            End If
        End If
    Next rng
    
    If Not matchFound Then
        MsgBox &quot;No matching hyperlink found.&quot;
    End If
End Sub

But the macro doesn't work when clicking on hyperlinks created with HYPERLINK() function, the macro should normally display "false" if the user does not click on the link but it does not work either.

Can you help me please?

答案1

得分: 1

这里有一种方法可以做到,不依赖于工作表事件,因为FollowHyperlink事件不会触发使用HYPERLINK函数创建的链接。我假设你的链接是指向网页的,但代码可以很容易地修改以允许链接到工作簿中的其他位置等。

  • 创建一个自定义函数(UDF),它执行两个操作:将相邻单元格的值设置为TRUE,并在浏览器中打开提供的URL。
  • HYPERLINK公式中链接到该函数,每次提供URL。

该函数如下所示:

Public Function GoToWebpageAndSetTrue(Webpage_Url)
    
    Set GoToWebpageAndSetTrue = Selection
    Selection.Offset(0, 1).Value = True
    ActiveWorkbook.FollowHyperlink Webpage_Url

End Function

应该很容易理解。将此代码插入工作簿中的一个模块以定义该函数。

现在,在你的超链接单元格中,你可以输入以下内容,例如:

=HYPERLINK("#GoToWebpageAndSetTrue(""https://www.google.com"")", "Link 1")

URL周围的双引号用于转义它们,因为它们位于另一对引号内。关于从HYPERLINK链接到UDF的方法,网上并没有太多的文档,我从这个答案中得到了这种方法。请注意答案中关于在第一行将函数设置为Selection的重要性的注意事项。

我创建了一个示例工作表,如下所示:

在链接被点击后,在相邻单元格中写入”true”。

点击链接2(单元格A2)后,我的屏幕如下所示:

在链接被点击后,在相邻单元格中写入”true”。

我相信这就是你想要实现的效果。

英文:

Here's a way to do it that doesn't rely on Worksheet events, since the FollowHyperlink event doesn't trigger for links created using the HYPERLINK function. I'm assuming that your links are to webpages, but the code could easily be modified to allow for links to other locations in the Workbook, etc.

  • Create a user-defined function (UDF) that does two things: sets the value of the adjacent cell to TRUE, and opens the URL provided in the browser
  • Link to the function from the HYPERLINK formula, providing the URL in each case.

The function looks like this:

Public Function GoToWebpageAndSetTrue(Webpage_Url)

    Set GoToWebpageAndSetTrue = Selection
    Selection.Offset(0, 1).Value = True
    ActiveWorkbook.FollowHyperlink Webpage_Url

End Function

Should be pretty easy to understand. Insert this code inside a module in the Workbook to define the function.

Now, in your hyperlink cells you can type the following, for example:

=HYPERLINK(&quot;#GoToWebpageAndSetTrue(&quot;&quot;https://www.google.com&quot;&quot;)&quot;, &quot;Link 1&quot;)

The double quotation marks around the URL are to escape them because they are inside another pair of quotation marks. There's not a lot of documentation online about linking to UDFs from HYPERLINK—I got the method from this answer. Note the caveat in the answer about the importance of setting the function to Selection in the first line.

I created an example Worksheet that looks like this:

在链接被点击后,在相邻单元格中写入”true”。

After clicking Link 2 (cell A2), my screen looks like this:

在链接被点击后,在相邻单元格中写入”true”。

which is believe is what you are looking to achieve.

huangapple
  • 本文由 发表于 2023年2月27日 12:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定