Find 1st Column with a Hyperlink

stuartgb100

Active Member
Joined
May 10, 2015
Messages
322
Office Version
  1. 2021
Platform
  1. Windows
I have code that inserts a row if a hyperlink is found:
Sub If_Hyperlink_Insert_Row()

VBA Code:
Dim ws As Worksheet
Dim c, Rng As Range

Set ws = ActiveSheet
    With ws
        'Amend for required column
        Set Rng = ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp))
        For Each c In Rng
            If c.Hyperlinks.Count > 0 Then ' has a hyperlink
                Rows(c.Row + 1).Insert
                Rows(c.Row + 1).RowHeight = 5
                Set c = c.Offset(1, 0)
            End If
        Next
    End With
End Sub

This uses a specified sheet & column.
I need to run this on all sheets in the activeworkbook (using For Each etc),
BUT the column containing the hyperlinks are different.

How do I find that column please ?

Thanks.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How do I find that column please ?
Your sheets have a Hyperlink member where you can extract all your hyperlinks. Each hyperlink in that collection will have a Range member where you can extract the column if that's what you want, but I suspect all you need is to get the link address and add a row below, so something like this should work:

VBA Code:
Sub fjlka()
    Dim ws As Worksheet, link As Hyperlink
    For Each ws In ThisWorkbook.Sheets
        For Each link In ws.Hyperlinks
            link.Range.Offset(1, 0).EntireRow.Insert
        Next link
    Next ws
End Sub
 
Upvote 0
Solution
If you still needed to know the first column with links, this would work
VBA Code:
Sub FindFirstColumnWithHyperlinkBySheet()
    Dim ws As Worksheet
    Dim link As Hyperlink
    Dim linksCount As Long
    Dim arr()
    Dim i As Long
    
    For Each ws In ThisWorkbook.Sheets
    
        linksCount = ws.Hyperlinks.Count
        
        If linksCount > 0 Then
            ReDim arr(1 To linksCount)
            i = 1
            
            For Each link In ws.Hyperlinks
                arr(i) = link.Range.Column
                i = i + 1
            Next link
            
            Debug.Print Application.WorksheetFunction.Min(arr)
            
        End If
        
        Erase arr
        
    Next ws
End Sub
 
Upvote 0
Yes, that's what I was looking for.
Many thanks again.
Regards.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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