Excel vba2007将注释复制为单元格值

时间:2018-01-22 09:55:04

标签: excel vba

我想将excel单元格(3,2)中的注释作为单元格值复制到使用VBA的同一工作簿的不同工作表的另一个单元格(7,5)。 我写了下面的代码,但它不起作用。实际上,以下代码正在复制它在sheet1中找到的第一条注释:

Set commentstrg = _ activeworkbook.sheets("sheet1").cells(3,2).special _ cells(xlcelltypecomments)
Getcomments = commentstrg.comment.text
Activeworkbook.sheets("sheet2").cells(7,5).value = getcomments

1 个答案:

答案 0 :(得分:0)

尝试:

With ActiveWorkbook
    .Sheets("sheet2").Cells(7,5) = .Sheets("sheet1").Cells(3,2).Comment.Text
End With

您可以将此转换为function(改编自@Chandoo),并添加代码以删除作者:

作为功能:

Function getComment(ByVal incell As Range) As String
' aceepts a cell as input and returns its comments (if any) back as a string
On Error Resume Next
getComment = incell.Comment.Text
End Function

删除作者:

=Right$(getcomment(inCell),Len(getcomment(inCell))-Application.WorksheetFunction.Find(":",getcomment(inCell)))

也可以写入函数:

Function getComment(ByVal incell As Range) As String
' aceepts a cell as input and returns its comments (if any) back as a string
On Error Resume Next
getComment = Right$(incell.Comment.Text, Len(incell.Comment.Text)-Len(incell.Comment.Author) + 1))
End Function

如果作为参数传递的范围中没有注释,则函数调用的优点是不会抛出错误。