Out of Memory

adavid

Board Regular
Joined
May 28, 2014
Messages
145
Below is my code for updating hyperlink addresses that were corrupted. I can update the hyperlinks in very small batches, but the sheet has almost 3000 rows. All of the hyperlinks are in column B. Any suggestions or code optimization would be very helpful.

Code:
Sub FixHLinks(sFind As String, sReplace As String, _
    Optional lStart As Long = 1, Optional lCount As Long = -1)
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim rCell As Range
    Dim hl As Hyperlink

'THIS WAS FOR TESTING PURPOSES
    For Each rCell In ActiveSheet.Range("B461:B561")
'THIS IS IDEAL
    'For Each rCell In ActiveSheet.UsedRange.Cells
        If rCell.Hyperlinks.Count > 0 Then
            For Each hl In rCell.Hyperlinks
                hl.Address = Replace(hl.Address, sFind, sReplace, lStart, lCount, vbTextCompare)
            Next hl
        End If
    Next rCell
 Set rCell = Nothing
 Set hl = Nothing
 Application.ScreenUpdating = True
 Application.Calculation = xlCalculationAutomatic
End Sub

'THE ABOVE CODE IS CALLED FROM THE CODE BELOW
Sub FixThem()
 Application.ScreenUpdating = False
 Application.Calculation = xlCalculationManual
    FindReplaceHLinks "../AppData/Roaming/Microsoft/Excel/", "\\NetworkShare\Folder1\Folder2\Folder3\"
 Application.ScreenUpdating = True
 Application.Calculation = xlCalculationAutomatic
End Sub
 
I modified your code for my column and paths. It just deleted all of the hyperlinks.

I rewrote the code to the code below and it worked for about 600 rows before I got the error that I ran out of memory.
Code:
Sub FixLinks(oString As String, nString As String, Optional lStart As Long = 1, Optional lCount As Long = -1)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
 
Dim i As Integer, j As Integer
Dim hLink As Hyperlink
Dim wSheet As Worksheet
Dim wRange As Range
    Set wRange = Sheet10.Range("B:B")
Dim rCell As Object
'Dim oString As String
    'oString = "../AppData/Roaming/Microsoft/Excel/"
'Dim nString As String
    'nString = "\\marnv006\Bm\Master Scheduling\PC 2.2.11 Work Authorizing Memorandum (WAMs)\"
   
     NumRows = Range("B3", Range("B3").End(xlDown)).Rows.Count
     Range("B3").Select
    ' Establish "For" loop to loop "numrows" number of times.
   
    '  For i = 1 To wRange.Rows(3, 2)
     '  With wRange
       For Each rCell In wRange
        If ActiveCell.Hyperlinks.Count > 0 Then
         For Each hLink In ActiveCell.Hyperlinks
            hLink.Address = Replace(hLink.Address, oString, nString, lStart, lCount)
         Next hLink
        End If
         ' Selects cell down 1 row from active cell.
         ActiveCell.Offset(1, 0).Select
       Next rCell
       'End With
      'Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
   End Sub
 
Sub PleaseWork()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
    FixLinks "../AppData/Roaming/Microsoft/Excel/", \\marnv006\Bm\Master Scheduling\PC 2.2.11 Work Authorizing Memorandum (WAMs)\
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

Hi, to recreate a set-up similar to yours I created a new blank workbook and ran the following code to create 3000 hyperlinks:

Code:
Sub SetUp()
Dim i As Long
Application.ScreenUpdating = False
Columns("A").Delete
For i = 1 To 3000
    ActiveSheet.Hyperlinks.Add Cells(i, 1), Address:="https://uk.search.yahoo.com/search?p=" & CStr(i), TextToDisplay:=CStr(i)
Next i
End Sub

I then ran this:
Code:
Sub FixThem()
FixHLinks "uk.search.yahoo.com/search?p=", "www.bing.com/search?q="
End Sub

With the FixHLinks code as:

Code:
Sub FixHLinks(sFind As String, sReplace As String, _
    Optional lStart As Long = 1, Optional lCount As Long = -1)
    
    Dim hl As Hyperlink
    
    For Each hl In ActiveSheet.Hyperlinks
        hl.Address = Replace(hl.Address, sFind, sReplace, lStart, lCount, vbTextCompare)
    Next hl


End Sub

And it executed in less than a second - if you do the same with a new blank workbook do you get the same sub second response without any memory errors?
 
Last edited:
Upvote 0

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.
it executed in less than a second - if you do the same with a new blank workbook do you get the same sub second response without any memory errors?

Did you try this test? What was the result?

I modified your code for my column and paths. It just deleted all of the hyperlinks.

Can you post the modified code?
 
Upvote 0
It's getting late in my little corner of the world so if no-one else jumps in in the meantime it will be tomorrow before I reply.
 
Upvote 0
FormR, here is the your code adapted with my edits.
Code:
Sub SetUp()
Dim i As Long
Application.ScreenUpdating = False
Columns("A").Delete
For i = 1 To 3000
    ActiveSheet.Hyperlinks.Add Cells(i, 1), Address:="../AppData/Roaming/Microsoft/Excel/" & CStr(i), TextToDisplay:=CStr(i)
Next i
End Sub


Sub FixThem()
FixHLinks "../AppData/Roaming/Microsoft/Excel/", "\\marnv006\Bm\Master Scheduling\PC 2.2.11 Work Authorizing Memorandum (WAMs)\"
End Sub


Sub FixHLinks(sFind As String, sReplace As String, _
    Optional lStart As Long = 1, Optional lCount As Long = -1)
    
    Dim hl As Hyperlink
    
    For Each hl In ActiveSheet.Hyperlinks
        hl.Address = Replace(hl.Address, sFind, sReplace, lStart, lCount, vbTextCompare)
    Next hl


End Sub
 
Upvote 0
FormR, here is the your code adapted with my edits.

Hi, the purpose of the code in post#10 was to create a test scenario - I was suggesting that you run the code unedited on a new blank workbook to see if it ran quickly without causing the memory error.
 
Upvote 0

Forum statistics

Threads
1,215,174
Messages
6,123,451
Members
449,100
Latest member
sktz

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