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
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi, instead of looping through all the of cells in the used range and checking if it has a hyperlink you could try looping through the hyperlinks directly.

For example:

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
 
Upvote 0
Thank you for the suggestion; unfortunately I still got the error. I suppose I'll just keep tweaking the code; if I get a positive result I will be sure to share.
 
Upvote 0
How many hyperlinks are there in the sheet?

Just a note: your calling code in post#1 is calling a different macro to that which you posted - did you change it to call the new code?
 
Upvote 0
Yes, I changed the code (I didn't notice until editing though so thank you).
For testing purposes I've isolated a cell with a hyperlink and it's still not working.
When I hover the cursor over a cell the hyperlink is shown as "file:///C:\Users\e301713\AppData\Roaming\Microsoft\Excel\Folder of File\File Name.pdf"
When I right click to edit the hyperlink it is shown as "..\AppData\Roaming\Microsoft\Excel\Folder%20of%20File%20\File%20Name.pdf"
Other hyperlinks may have ""../AppData/Roaming/Microsoft/Excel/Folder%20of%20File%20/File%20Name.pdf"

I need the Folder of File and File Name.pdf to be maintained. I'm just trying to correct the rest of the path.
Code:
Sub FixHLinks(sFind As String, sReplace As String, _
    Optional lStart As Long = 1, Optional lCount As Long = -1)
    
    Dim hl As Hyperlink
    Dim hlRange As Range
        Set hlRange = Sheet10.Range("B23832")
    
    For Each hl In hlRange.Hyperlinks
        hl.Address = Replace(hl.Address, sFind, sReplace, lStart, lCount, vbTextCompare)
    Next hl
 Set hlRange = vbNullString
End Sub
 
Upvote 0
For testing purposes I've isolated a cell with a hyperlink and it's still not working.

Are you saying you get the "out of memory" error when updating just one hyperlink?
 
Upvote 0
Sorry, I should've clarified that. I do not receive the out of memory error when testing on the one cell.
I elaborated on the cell's differing link structure because I was not sure if that was a part of the problem.

Are you saying you get the "out of memory" error when updating just one hyperlink?
 
Upvote 0
I elaborated on the cell's differing link structure because I was not sure if that was a part of the problem.

I very much doubt it's got anything to do with the out of memory problem - how many hyperlinks are on the sheet?

Code:
MsgBox ActiveSheet.Hyperlinks.Count
 
Upvote 0
Thanks for the code. There are 2593 hyperlinks. Not all of them are corrupt.

I very much doubt it's got anything to do with the out of memory problem - how many hyperlinks are on the sheet?

Code:
MsgBox ActiveSheet.Hyperlinks.Count
 
Upvote 0
There are 2593 hyperlinks.

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?
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,148
Members
448,552
Latest member
WORKINGWITHNOLEADER

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