简体   繁体   English

如何基于范围为单元格分配多个单元格值作为注释

[英]How to assign multiple cell values as comment to cell based on Range

I want to run a macro that will assign multiple cell values from one sheet as a comment in cells on another sheet, based on range and value. 我想运行一个宏,该宏将根据范围和值从一个工作表分配多个单元格值作为另一工作表的单元格中的注释。

工作表1 工作表2

So in Sheet1 , I want to select range B1:D4 , then for each cell, if => 0 , add the corresponding comment from Sheet2 containing serial number, operation, and quantity. 因此,在Sheet1 ,我想选择范围B1:D4 ,然后对于每个单元格, if => 0 ,则从Sheet2中添加相应的注释,其中包含序列号,操作和数量。

edit 编辑

EDIT2 编辑2

Sub COMMENTS()
'
' COMMENTS Macro
    Dim rngCell As Range
    Dim strComment, strStep, strObject As String, strConcat As String
    Dim varMatch As Variant
    Dim arrConcat() As String

    For Each rngCell In Sheet2.Range("E2:E30")
        strConcat = strConcat & rngCell & rngCell.Offset(0, -4) & "||"
    Next rngCell

    arrConcat = Split(strConcat, "||")

    For Each rngCell In Sheet1.Range("B2:D5")
        If rngCell > 0 Then
            strStep = Right(Sheet1.Cells(rngCell.Row, 1).Value, 1)
            strObject = Sheet1.Cells(1, rngCell.Column).Value
            varMatch = Application.Match(strStep & strObject, arrConcat, 0)
            If Not IsError(varMatch) Then
                With Sheet2
                    strComment = "Serial number: " & .Range("B" & varMatch + 1).Value & Chr(10) _
                        & "Operation: " & .Range("C" & varMatch + 1).Value & Chr(10) _
                        & "Quantity: " & .Range("D" & varMatch + 1).Value
                End With
                rngCell.AddComment (strComment)
            End If
        End If
    Next rngCell
End Sub

Give it a try: 试试看:

Sub COMMENTS()
    Dim rngCell As Range
    Dim strComment, strStep, strObject As String, strConcat As String
    Dim varMatch As Variant
    Dim arrConcat() As String

    For Each rngCell In Sheet2.Range("E2:E9")
        strConcat = strConcat & rngCell & rngCell.Offset(0, -4) & "||"
    Next rngCell

    arrConcat = Split(strConcat, "||")

    For Each rngCell In Sheet1.Range("B2:D5")
        If rngCell > 0 Then
            strStep = Right(Sheet1.Cells(rngCell.Row, 1).Value, 1)
            strObject = Sheet1.Cells(1, rngCell.Column).Value
            varMatch = Application.Match(strStep & strObject, arrConcat, 0)
            If Not IsError(varMatch) Then
                With Sheet2
                    strComment = "Serial number: " & .Range("B" & varMatch + 1).Value & Chr(10) _
                        & "Operation: " & .Range("C" & varMatch + 1).Value & Chr(10) _
                        & "Quantity: " & .Range("D" & varMatch + 1).Value
                End With
                rngCell.AddComment (strComment)
            End If
        End If
    Next rngCell
End Sub

Result: 结果:

在此处输入图片说明

Note that there is no combination of "Step 4" and "y" present in Sheet2, that is why 4 in cell C5 does not show any comment. 请注意,Sheet2中不存在“步骤4”和“ y”的组合,这就是为什么单元格C5中的4不显示任何注释的原因。 The code will also fail if there is already a comment added to a given cell (this can be also future-proofed). 如果已经在给定的单元格中添加了注释,则代码也将失败(这也可以用于将来)。

Edit: 编辑:

In case there is more than one match in Sheet2: 如果Sheet2中有多个匹配项:

Sub COMMENTS()
    Dim rngCell As Range
    Dim strComment As String, strStep As String, strObject As String, strConcat As String
    Dim arrConcat() As String
    Dim lngPos As Long

    For Each rngCell In Sheet2.Range("E2:E13")
        strConcat = strConcat & rngCell & rngCell.Offset(0, -4) & "||"
    Next rngCell

    arrConcat = Split(strConcat, "||")

    For Each rngCell In Sheet1.Range("B2:D5")
        If rngCell.Value >= 0 Then
            strStep = Right(Sheet1.Cells(rngCell.Row, 1).Value, 1)
            strObject = Sheet1.Cells(1, rngCell.Column).Value
            For lngPos = 0 To UBound(arrConcat)
                If LCase$(strStep & strObject) = LCase$(arrConcat(lngPos)) Then
                    With Sheet2
                        strComment = strComment & Chr(10) _
                            & "Serial number: " & .Range("B" & lngPos + 2).Value & Chr(10) _
                            & "Operation: " & .Range("C" & lngPos + 2).Value & Chr(10) _
                            & "Quantity: " & .Range("D" & lngPos + 2).Value
                    End With
                End If
            Next lngPos
            rngCell.ClearComments
            If Len(strComment) Then
                rngCell.AddComment (Right(strComment, Len(strComment) - 1))
                rngCell.Comment.Shape.TextFrame.AutoSize = True
            End If
            strComment = vbNullString
        End If
    Next rngCell
End Sub

在此处输入图片说明

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

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