英文:
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
中时才起作用,而我想要:
- VBA代码可以处理由超链接函数创建的链接。
- 只要用户没有点击链接,宏必须在相邻单元格中显示“false”,否则显示“true”。
- 此宏必须能够应用于包含超文本链接的所有单元格,无论它们是如何创建的。
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("A1").Address Then
Range("B1").Value = "True"
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:
- VBA code works with a link created by the hyperlink function.
- As long as the user has not clicked on the link, the macro must display "false" in the adjacent cell and "true" otherwise.
- This macro must be able to be applied to all cells containing hypertext links regardless of how they were created.
friendly_name
in thehyperlink()
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 > 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 "No matching hyperlink found."
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
的重要性的注意事项。
我创建了一个示例工作表,如下所示:
点击链接2(单元格A2)后,我的屏幕如下所示:
我相信这就是你想要实现的效果。
英文:
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("#GoToWebpageAndSetTrue(""https://www.google.com"")", "Link 1")
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:
After clicking Link 2 (cell A2), my screen looks like this:
which is believe is what you are looking to achieve.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论