簡體   English   中英

從條件添加相鄰單元格值的注釋

[英]Add comment from adjacent cell value with a condition

我沒有VBA經驗。 我知道有很多與此問題類似的答案,但是我無法調整任何代碼來讓它對我有用。

我有一個Excel工作表,其中表中有很多行。
A列中有一些值(數字),B列中有它們的注釋(文本)。我想將這些注釋(B列)作為對A列單元格的注釋。

但這是條件:
A列中的某些單元格已經有注釋,因此我不想將其替換為注釋。 因此,我需要一個可以跳過這些特定單元格或將其注釋與注釋合並的代碼。

在這里,我為您解決的問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM