2015-07-19 75 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

你有沒有試過我的答案?如果它不適合你,請告訴我。如果是工作,請標記爲答案。謝謝。 –

相關問題