Can I use VBA to make Excel forget that it followed a hyperlink?

纵饮孤独 提交于 2020-02-22 07:53:06

问题


I'm using conditional formatting to affect font color on certain cells containing hyperlinks in a table. When I follow the hyperlink, though, it overrides the conditional formatting in the cell, which is undesired.

The conditional formatting is essentially making the text invisible if the cell immediately above the current cell has the same value. It uses a formula (=C5=C6) applied throughout the range ($C$6:$C$24).

Things I've tried:

  • Closing and re-opening the file fixes the issue because it resets all the followed hyperlinks back to normal hyperlinks. However, I don't want to have to close and re-open all the time.
  • I can use the Worksheet_FollowHyperlink event to change the font color back, but when I sort the table differently (thus changing how the conditional formatting should act), the conditional formatting is still disabled -- now in favor of the programatically-applied font color.

This question is essentially an update of my previous post (Is there a way to make Conditional Formatting ignore the FollowedHyperlink flag?). I'm no longer using the HYPERLINK formula, since that wasn't allowing me to use the Worksheet_FollowHyperlink option provided in an answer.

I've also read through the provided SuperUser thread (https://superuser.com/questions/728825/how-do-i-prevent-excel-from-changing-the-color-of-a-clicked-link/819931), which provided some other interesting avenues to pursue, but didn't quite get me where I needed to be. I'm specifically asking now if I can reset Excel's memory of which hyperlinks have been followed.

This is want I want, both before and after clicking the link:

This is the undesired behavior that I get after clicking the link:


回答1:


You haven't disclosed what format you are using in your CF, but I'm guessing it's setting text colour to white.

A Far better general format for hiding a cells contents is ;;;. If you use this in your CF, then a followed hyperlink remains hidden




回答2:


Something like the following seems to work to keep the style of the hyperlink unchanged - basically just re-add the hyperlink to the same cell:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    With Target
        .Range.Hyperlinks.Add .Range, .Address, .SubAddress, , .TextToDisplay
    End With
End Sub

ScreenTip was causing issues so I intentionally omitted it - the hyperlink I tested it with had no screen tip.

If in fact this does work for your case, to make it more robust I would test the length of the SubAddress, ScreenTip, and TextToDisplay before attempting to use them in Hyperlinks.Add. They are all optional parameters, and the hyperlink clicked might not have those properties to begin with.




回答3:


This worked for me and did not interfere with conditional formatting:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    With Target.Range
        .Font.Color = vbBlue
        .Font.Underline = True
    End With

End Sub



回答4:


So, here's a work-around that I just figured out...finally. Still not the best answer, and still doesn't answer the specific question of how to make Excel forget that it followed a specific link, so I'd love to still see better answers.

But this gets done what I wanted to get done...for the most part.

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    Target.Range.Style = "Followed Hyperlink"

End Sub

Setting the style to "Followed Hyperlink" with the code above, surprisingly does the trick. As noted in previous comments, Excel isn't using this style even though it looks like it is. Setting it manually with VBA apparently allows the conditional formatting to function.

Downside: closing and opening the workbook no longer resets the color. A workaround would be to change the "Followed Hyperlink" style to the same as the "Hyperlink" style, but then no links throughout the workbook would have the purple color. So, this is still not the most optimal answer.

EDIT: I should make it clear that using this option applies the "Followed Hyperlink" style (purple text). It doesn't override the conditional formatting, though, which was the biggest part of the problem.



来源:https://stackoverflow.com/questions/60101044/can-i-use-vba-to-make-excel-forget-that-it-followed-a-hyperlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!