0

给定一个简单的 excel 示例文件复制单元格的值、结构和超链接的 VBA 代码是什么?

即在单元格 B5 我们将调用=myCopyCellFunction(B2),我们将在 B2 处获得确切的单元格值

在此处输入图像描述

4

1 回答 1

1

在普通代码模块中试试这个

Function copyIt(ByVal vRange As Range) As String
Dim sAddress As String
Dim sText As String
'
Application.EnableEvents = False

Application.ThisCell.Hyperlinks.Delete
' handle non hyperlinks in source cell
If vRange.Hyperlinks.Count < 1 Then
    copyIt = vRange.Text
    Exit Function
End If
'
' get values for new hyperlink in target cell
sAddress = vRange.Hyperlinks(1).Address
sText = vRange.Hyperlinks(1).TextToDisplay
'
' apply link to target cell
Application.ThisCell.Hyperlinks.Add Anchor:=Application.ThisCell, Address:=sAddress, TextToDisplay:=sText
' return string value 
copyIt = sText
'    
Application.EnableEvents = True

End Function

然后你在这样的单元格中使用它:

=copyIt(B2)

于 2013-05-14T08:56:38.727 回答