Trying to speed up my Hyperlink restoration macro

jmpatrick

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

I've been using this code to restore the colors of several thousand hyperlinks on my sheet. It gets the job done but it's too darn slow. Here's the code:

VBA Code:
Sub RestoreFormatting()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
  
    Dim hl As Hyperlink
        
    'Restore Hyperlink Color
    For Each hl In Range("CalendarHyperlinks").Hyperlinks
    hl.Range.Font.Color = RGB(0, 0, 255)
    Next
    
    'Restore SubLotColumn Hyperlinks
    For Each hl In Range("SubLotColumn").Hyperlinks
    hl.Range.Font.Color = RGB(0, 0, 0)
    Next
    
    'Restore CalendarFieldManagersColumn Color
    For Each hl In Range("CalendarFieldManagersColumn").Hyperlinks
    hl.Range.Font.Color = RGB(0, 0, 0)
    Next
    
    'Restore CalendarLinkRow Color
    For Each hl In Range("CalendarLinkRow").Hyperlinks
    hl.Range.Font.Color = RGB(0, 0, 255)
    Next
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I think this can go no faster than this:
VBA Code:
Sub RestoreFormatting()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
 
    Range("CalendarHyperlinks,CalendarLinkRow").Font.Color = RGB(0, 0, 255)

    Range("SubLotColumn,CalendarFieldManagersColumn").Font.Color = RGB(0, 0, 0)
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 
Upvote 0
I think this can go no faster than this:
VBA Code:
Sub RestoreFormatting()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
 
    Range("CalendarHyperlinks,CalendarLinkRow").Font.Color = RGB(0, 0, 255)

    Range("SubLotColumn,CalendarFieldManagersColumn").Font.Color = RGB(0, 0, 0)
  
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
  
End Sub

That works but it reformats everything in the named range, not just the hyperlinks.
 
Upvote 0
You are right. A possible workaround could be:
VBA Code:
Sub RestoreFormatting()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
 
   
    Range("CalendarHyperlinks").AutoFilter Field:=1, Criteria1:="=http*", Operator:=xlAnd
    Range("CalendarHyperlinks").Font.Color = RGB(0, 0, 255)
    AutoFilterMode = False
    Range("CalendarLinkRow").AutoFilter Field:=1, Criteria1:="=http*", Operator:=xlAnd
    Range("CalendarLinkRow").Font.Color = RGB(0, 0, 255)
    AutoFilterMode = False
    Range("SubLotColumn").AutoFilter Field:=1, Criteria1:="=http*", Operator:=xlAnd
    Range("SubLotColumn").Font.Color = RGB(0, 0, 0)
    AutoFilterMode = False
    Range("CalendarFieldManagersColumn").AutoFilter Field:=1, Criteria1:="=http*", Operator:=xlAnd
    Range("CalendarFieldManagersColumn").Font.Color = RGB(0, 0, 0)
    AutoFilterMode = False

    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
   
End Sub
 
Upvote 0
I suggest this:
VBA Code:
Sub RestoreFormatting()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
  
    Dim hl As Hyperlink
    Dim rng As Range

    For Each hl In Range("
CalendarHyperlinks,CalendarLinkRow,SubLotColumn,CalendarFieldManagersColumn").Hyperlinks
        If rng Is Nothing Then
            Set rng = hl.Range
        Else
            Set rng = Union(rng, hl.Range)
        End If
    Next
    If Not rng Is Nothing Then rng.Font.Color = vbBlack
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 
Upvote 1
Solution
I suggest this:
VBA Code:
Sub RestoreFormatting()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
 
    Dim hl As Hyperlink
    Dim rng As Range

    For Each hl In Range("
CalendarHyperlinks,CalendarLinkRow,SubLotColumn,CalendarFieldManagersColumn").Hyperlinks
        If rng Is Nothing Then
            Set rng = hl.Range
        Else
            Set rng = Union(rng, hl.Range)
        End If
    Next
    If Not rng Is Nothing Then rng.Font.Color = vbBlack
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
   
End Sub

Thanks to both of you for the help.

This works.
 
Upvote 0

Forum statistics

Threads
1,215,167
Messages
6,123,401
Members
449,098
Latest member
ArturS75

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