VBA to copy hyperlink

RetroManHD

New Member
Joined
Jun 17, 2016
Messages
28
Hi guys,

I have a line of code in my macro as below:

The code is: For Each myCell In Sh.Range("J9,D9,D13,O66,D8,A1")

There is more to the macro, but this line basically copies the information of cells from one sheet to cells in another sheet. The problem I'm having is that the final cell 'A1' is a hyperlink to a file on my computer and the hyperlink does not copy across, but the text in the cell does.

I'm wondering does anyone know how I can change the code above so the hyperlink copies across.

Thanks,
Steven.
 
Then you'll need a UDF in the file to extract the actual path of the hyperlink. So you could add this:
Code:
Function HLinkPath(Cell As Range) As String
    On Error Resume Next
    HLinkPath = Cell(1).Hyperlinks(1).Address
End Function
to your module. Then alter your current code to this:
Code:
    For Each myCell In Sh.Range("J9,D9,D13,O66,D8,A1")
    
    
    ColNum = ColNum + 1
    if mycell.address(0,0) = "A1" then
Newsh.Cells(RwNum, ColNum).Formula = _
    "=HYPERLINK(HLinkPath('" & Sh.Name & "'!" & myCell.Address(False, False) & "))
    else
    Newsh.Cells(RwNum, ColNum).Formula = _
    "='" & Sh.Name & "'!" & myCell.Address(False, False)
    end if       
        Next myCell

I'll give this a try and let you know...

Thanks, Rory.
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Apologies, Rory, but could you advise as to where in my current code I would copy the Function code, also when I paste the altered code, I keep getting a Next without For error?!?!
 
Upvote 0
The function code needs to be in a normal module (doesn't matter which) not a worksheet or ThisWorkbook code module.

the other code replaces this in your original:
Code:
    For Each myCell In Sh.Range("J9,D9,D13,O66,D8,A1")
    
    
    ColNum = ColNum + 1
    Newsh.Cells(RwNum, ColNum).Formula = _
    "='" & Sh.Name & "'!" & myCell.Address(False, False)
                
        Next myCell
 
Upvote 0

Forum statistics

Threads
1,223,738
Messages
6,174,213
Members
452,551
Latest member
croud

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