Creating Hyperlinks, but ONLY in cells that contain a value

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
Hello!

I have this code that creates Hyperlinks in every cell in a named range. I'm trying (without luck) to ONLY create a link if there's a value in the cell. In other words, skip blank cells. Here's my code so far:

VBA Code:
Sub AddJobEditHyperlinks()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    'Add Notes Hyperlinks
    Dim cl As Range
    For Each cl In Range("SubLotColumn", Range("C" & Rows.Count).End(xlUp))
    ActiveSheet.Hyperlinks.Add Anchor:=cl, Address:="", SubAddress:= _
        cl.Address, ScreenTip:="Click To Open Job Edit Window"
    Next cl
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Not sure if this is what you want ...
Rich (BB code):
Sub AddJobEditHyperlinks()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    'Add Notes Hyperlinks
    Dim cl As Range
    For Each cl In Range("SubLotColumn", Range("C" & Rows.Count).End(xlUp))
        If cl.Formula <> vbNullString Then
            ActiveSheet.Hyperlinks.Add Anchor:=cl, Address:="", SubAddress:=cl.Address, ScreenTip:="Click To Open Job Edit Window"
        End If
    Next cl
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 
Upvote 0
Not sure if this is what you want ...
Rich (BB code):
Sub AddJobEditHyperlinks()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    'Add Notes Hyperlinks
    Dim cl As Range
    For Each cl In Range("SubLotColumn", Range("C" & Rows.Count).End(xlUp))
        If cl.Formula <> vbNullString Then
            ActiveSheet.Hyperlinks.Add Anchor:=cl, Address:="", SubAddress:=cl.Address, ScreenTip:="Click To Open Job Edit Window"
        End If
    Next cl
 
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
 
End Sub

Thanks.

No errors, but it's creating a Hyperlink in every cell whether it has a value or not. There's no Formulas in the cells, just text data.
 
Upvote 0
Try changing this line:
VBA Code:
        If cl.Formula <> vbNullString Then
to this:
VBA Code:
       If cl.Value <> "" Then
 
Upvote 0
Solution
Regardless of whether a cell contains a formula or just text, its Formula property will always have a value. Check your cells for spaces.
The code change below eliminates hidden spaces.
VBA Code:
        If VBA.Trim(cl.Formula) <> vbNullString Then
 
Upvote 0

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,560
Latest member
Torchwood72

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