VBA excel,工作表副本但超链接已更改

问题描述 投票:2回答:2

当我尝试将整个工作表复制到另一个工作表时,除了将超链接更改为以“........ \”而不是带有超链接的单元格中的“http:\”启动时,一切都很好。

复制方法很简单。我不知道为什么会这样。以下是我的简单代码,将temp1Workbook的“temp1sheet”复制到PrimaryWorkbook的“LatestData”表。

'   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

感谢您的建议和帮助。

excel vba excel-vba hyperlink
2个回答
0
投票

我认为您在Code中几乎不需要修改。更好的方法是创建一个Variable并传输Cell值并在所需位置粘贴。

例如,,

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

注意:需要将目标单元格转换为超链接单元格。

希望这对你有所帮助。


0
投票

将工作表的整个部分从一个工作簿复制到另一个工作簿时,我建议您将包含数据的实际列从一个工作表复制到另一个工作表的相应列。这可确保您捕获所有格式和超链接。

此外,复制后,我建议您只重新复制值,因为如果您的源表中有公式引用源工作簿中的其他工作表,那么目标工作表中的跨工作簿公式引用将是最好的如果不需要则避免。

恕我直言,通常最好避免在代码中使用.Activate和对活动工作簿/工作表的引用,除非您希望用户以某种方式与工作表进行交互。仅在操作代码中的工作表内容时,最好使用对工作簿/工作表对象本身的引用。

这是代码:

'// 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
© www.soinside.com 2019 - 2024. All rights reserved.