Fixing an hyperlink vba macro

MasterBash

New Member
Joined
Jan 22, 2022
Messages
49
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello,

I currently have this :

VBA Code:
Option Explicit
    Dim location As String
    Dim subject As String
    Dim body As String
    Dim email As String

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

If Range(Target.SubAddress).Column = 3 Then
    location = Range("B" & Range(Target.SubAddress).Row).Value
    email = Range("G1")
    subject = Range("G2") & location
    body = Range("G3") & location & Range("H3")
   
    Call Send_The_Emails
End If
End Sub

What it does :
When I click on a link in the third column (C), it calls Send_The_Emails.

What I am trying to do :
Have 2 different "If... End if" depending on which row I choose to click in the C column.

Something like this :
VBA Code:
Option Explicit
    Dim location As String
    Dim subject As String
    Dim body As String
    Dim email As String

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

If Range(Target.SubAddress) = "C1;C24" Then
    location = Range("B" & Range(Target.SubAddress).Row).Value
    email = Range("G1")
    subject = Range("G2") & location
    body = Range("G3") & location & Range("H3")
   
    Call Send_The_Emails_1
End If

If Range(Target.SubAddress) = "C26;C28" Then
    location = Range("B" & Range(Target.SubAddress).Row).Value
    email = Range("F26")
    subject = Range("F27") & location
    body = Range("F28")
    Call Send_The_Emails_2
End If
End Sub

If I click C1 to C24, then it must call Send_The_Emails_1 and if I click C26 to C28, it must call Send_The_Emails_2

Well, I am posting here because it doesn't work. The "If... Then" seems incorrect because it doesn't do anything. The first script works fine.

Can anyone please help me out ? Thank you. :)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
As I interpret this I believe it's the target.range.address you need to be looking at not the subaddress
 
Upvote 0
I'm just guessing that the hyperlinks in each cell refer to themselves, because otherwise the description of how the first version of the code works would not match.
Target.SubAddress is the address of the reference target, not the address of the cell in which the hyperlink you clicked is located. The only exception is when the hyperlink references itself.
To find out which cell was clicked on, examine Target.Range. The notation:
VBA Code:
If Range(Target.SubAddress) = "C1;C24" Then
is incorrect. By omitting the semicolon used, a condition was created: Does the clicked cell have the value "C1;C24"?
So the condition should be created similar to how it is created in the _Change and _SelectionChange events:
VBA Code:
If Not Intesect(Target.Range, Me.Range("C1:C24")) Is Nothing Then

Artik
 
Upvote 0
Solution
So the condition should be created similar to how it is created in the _Change and _SelectionChange events:
VBA Code:
If Not Intesect(Target.Range, Me.Range("C1:C24")) Is Nothing Then
Intersect* in case anyone is interested and comes across this thread. :)

Thank you so much ! It works. :) I appreciate it !
 
Upvote 0

Forum statistics

Threads
1,221,283
Messages
6,159,018
Members
451,531
Latest member
Ian Denney

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top