简体   繁体   English

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

[英]Excel vba2007 copy comment as cell values

I want to copy comments from excel cells(3,2) as cell values to another cells(7,5) of different sheet of same workbook using VBA. 我想使用VBA将Excel单元格(3,2)中的注释作为单元格值复制到同一工作簿的不同工作表的另一个单元格(7,5)中。 I have written the following code but it is not working. 我已经编写了以下代码,但无法正常工作。 In fact, the following code is copying the first comment it found in sheet1: 实际上,以下代码正在复制它在sheet1中找到的第一个注释:

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

Try: 尝试:

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

You could convert this to a function (adapted from @Chandoo) and also add code to remove the author: 您可以将其转换为函数 (改编自@Chandoo),还可以添加代码以删除作者:

As function: 作为功​​能:

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

Remove author: 删除作者:

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

Which could also be written into the function as: 也可以将其写为函数:

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

The function calls having the advantage of not throwing an error if no comment is present in the range passed as an argument. 该函数调用的优点是,如果在作为参数传递的范围内没有注释,则不会引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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