Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用条件从相邻单元格值添加注释_Vba_Excel - Fatal编程技术网

Vba 使用条件从相邻单元格值添加注释

Vba 使用条件从相邻单元格值添加注释,vba,excel,Vba,Excel,我没有VBA方面的经验。我知道有很多类似于这个问题的答案,但我不能调整任何代码来让它为我工作 我有一张excel表格,表格中有大量行。在a列中有一些值(数字),在B列中有它们的注释(文本)。我想将这些注释(B列)作为对a列单元格的注释。 但条件是: A列中的某些单元格已经有注释,我不想用注释替换它们。因此,我需要一个代码,可以跳过这些特定的单元格,也可以将它们的注释与注释合并。这里是我解决问题的方法: Public Sub addComment() Dim row As Integer

我没有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
Public Sub addComment()
将行设置为整数
将注释设置为字符串
'设置起始行
行=1
带图纸(“图纸名称”)
'直到“A”单元格为空
Do While.Range(“A”行和“行”)
'如果“B”单元格不是空的
如果.Range(“B”和行)”,则
'如果“A”没有注释,请将“”设置为oldComment
If.Range(“A”&行).Comment则为空
oldComment=“”
“否则,评论将不存在
其他的
'将该注释存储到oldComment
oldComment=.Range(“A”行和行).Comment.Text&“
'从单元格中删除注释
.Range(“A”行和行).Comment.Delete
如果结束
'为“A”插入注释,如果存在旧注释
.Range(“A”和行).addComment(oldComment和.Range(“B”和行).Value)
如果结束
"增行"
行=行+1
环
以
端接头

这里是我解决您问题的方法:

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
Public Sub addComment()
将行设置为整数
将注释设置为字符串
'设置起始行
行=1
带图纸(“图纸名称”)
'直到“A”单元格为空
Do While.Range(“A”行和“行”)
'如果“B”单元格不是空的
如果.Range(“B”和行)”,则
'如果“A”没有注释,请将“”设置为oldComment
If.Range(“A”&行).Comment则为空
oldComment=“”
“否则,评论将不存在
其他的
'将该注释存储到oldComment
oldComment=.Range(“A”行和行).Comment.Text&“
'从单元格中删除注释
.Range(“A”行和行).Comment.Delete
如果结束
'为“A”插入注释,如果存在旧注释
.Range(“A”和行).addComment(oldComment和.Range(“B”和行).Value)
如果结束
"增行"
行=行+1
环
以
端接头

您不必看到您是如何尝试调整代码的?也许你可以问一个关于你在试图解决问题时遇到的问题的更集中的问题。你不必看到你是如何尝试调整代码的?也许你可以问一个关于你在解决问题时遇到的问题的更集中的问题。你试过我的答案吗?如果对你不起作用,请告诉我。如果是工作,则标记为答案。谢谢。你试过我的答案了吗?如果对你不起作用,请告诉我。如果是工作,则标记为答案。谢谢