简体   繁体   中英

VBA excel, sheet copy but hyperlink is changed

When I try to copy the whole sheet to another, every thing is fine except the hyperlink is changed to be started with "........\\" instead with "http:\\" in cells with hyperlink.

The copy method is simple. I dont know why it happened. The following is my simple codes, copying sheet "temp1sheet" of temp1Workbook to sheet "LatestData" of PrimaryWorkbook.

'   copy data into the "master file"
Windows(temp1Workbook).Activate
Sheets(temp1Sheet).Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'   paste result, now there is a fresh list of Active
Windows(PrimaryWorkbook).Activate
Sheets("LatestData").Select
Range("A1").Select
ActiveSheet.Paste

Thanks for suggestion and help in advance.

I think you need little modification in Code. Better way is to create a Variable and transfer Cell value and Paste at required place.

For Example,,

If Worksheets("Sheet1").Range("A1).Value > "" Then

    Dim NewHLnk As String

    If Worksheets("Sheet1").Range("A1").Hyperlinks.Count = 1 Then

    NewHLnk = Worksheets("Sheet1").Range("A1").Hyperlinks(1).Address 

    Worksheets("Sheet2").Range("A1").Hyperlinks.Add Anchor:=Worksheets("Sheet2").Range("A1), Address:=Worksheets("Sheet2").Range("A1") 
    Worksheets("Sheet2").Range("A1").Hyperlinks(1).Address = NewHLnk  

    End If
 End if

NB: The need is to turn the destination cell into Hyperlink Cell.

Hope this help you.

When copying whole sections of a worksheet from one workbook to another I suggest you copy the actual columns containing data from one worksheet to the corresponding columns of the other. This makes sure that you capture all the formatting and hyperlinks.

Also, after copying, I suggest you re-copy just the values, because if you have formulas in your source sheet that refer to other worksheets in the source workbook, then there will be cross-workbook formula references in the destination worksheet that are best avoided if not needed.

IMHO, it is generally best to avoid using .Activate and references to active workbooks/worksheets in your code unless you are expecting to the user to interact with the sheets in some way. When only manipulating the worksheet content within the code, it is better to use references to the workbook / worksheet objects themselves.

Here is the code:

'// Get reference to source worksheet to be copied
Dim shtFrom As Worksheet
Set shtFrom = Application.Workbooks(temp1Workbook).Sheets(temp1Sheet)

'// Get reference to destination worksheet
Dim shtTo As Worksheet
Set shtTo = Application.Workbooks(PrimaryWorkbook).Sheets("LatestData")

'// Get the address of the last used cell in the source worksheet
Dim sLastCell As String
sLastCell = shtFrom.UsedRange.SpecialCells(xlCellTypeLastCell).Address

'// References to the range (of columns) to be copied from / to
Dim rFrom As Range
Dim rTo As Range

'// Get the full range of columns to be copied (col 1 to last used col)
Set rFrom = shtFrom.Range("A1:" & sLastCell).EntireColumn

'// Get the destination columns (i.e. same columns of destination worksheet)
Set rTo = shtTo.Range(rFrom.Address)

'// Clear the destination worksheet of all content
shtTo.Cells.Clear

'// Copy the source columns
rFrom.Copy

'// Paste all first to get formats and hyperlinks etc
rTo.PasteSpecial xlPasteAll

'// Overwrite all formulas with values to ensure no workbook cross-references
rTo.PasteSpecial xlPasteValues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM