2015-07-19 59 views
0

我对VBA没有经验。我知道有很多类似于这个问题的答案,但我无法调整任何代码来让它为我工作。用条件添加相邻单元格值的注释

我有一个Excel表中有大量行的Excel表。
列A中有一些值(数字)和列B中的备注(文本)。我想将这些注释(列B)作为​​A列中单元格的注释。

但是,这里是条件:
A列中的一些单元格已经对它们有评论,我不想用笔记替换它们。所以我需要一个代码,可以跳过这些特定的单元格或将它们的注释与注释合并。

+0

你不必[包括在你的标题“标签”(http://meta.stackexchange.com/questions/19190/should- questions-include-tags-in-their-titles)另请参阅[帮助中心](http://stackoverflow.com/help/tagging) – 0m3r

+1

您是如何试图调整代码的?也许你可以问一个关于当你试图解决它时遇到的问题的更有针对性的问题。 –

回答

4

在这里,我对你的问题的方法:

Public Sub addComment() 

    Dim row As Integer 
    Dim oldComment As String 

    'Set start row 
    row = 1 

    With Sheets("sheetname") 

     'Do until "A" cell is blank 
     Do While .Range("A" & row) <> "" 

      'If "B" cell is not blank 
      If .Range("B" & row) <> "" Then 

       'If "A" has no comment, set "" to oldComment 
       If .Range("A" & row).Comment Is Nothing Then 
        oldComment = "" 

       'Else comment is exist 
       Else 

        'Store that comment to oldComment 
        oldComment = .Range("A" & row).Comment.Text & " " 

        'Delete comment from cell 
        .Range("A" & row).Comment.Delete 

       End If 

       'Insert comment for "A" with old if exist 
       .Range("A" & row).addComment (oldComment & .Range("B" & row).Value) 

      End If 

      'Increase row 
      row = row + 1 

     Loop 

    End With 

End Sub 
+0

你有没有试过我的答案?如果它不适合你,请告诉我。如果是工作,请标记为答案。谢谢。 –

相关问题