简体   繁体   English

将多个单元格值插入评论

[英]Insert multiple cell values to a comment

I have a task to add a comment in a sumtotal by month to show the items involved and the amount.我的任务是按月在总计中添加评论以显示所涉及的项目和金额。 I Want to simplify the works because Ido it by monthly and every job (over 20 jobs)!我想简化工作,因为我按月和每个工作(超过 20 个工作)来做!

I found solutions for a single cell.我找到了单个细胞的解决方案。

I need to add related cell values to one comment for the month.我需要将相关的单元格值添加到该月的一条评论中。
在此处输入图像描述

Option Explicit

Sub CreateComment()

    Dim rng As Range
    Dim cel As Range
    Dim myColumn, myRow As Integer
    
    Set rng = Selection
    myColumn = ActiveCell.Column
    myRow = ActiveCell.Row
    
        
    For Each cel In rng
        If cel.Value <> "" Then
        Range("myColumn" & "1").AddComment [Cell("myRow", "1")).Value & " -$" & Cell("myRow","myColumn")_.value]
        End If
    Next
                
End Sub

Add Comments to a Range向范围添加注释

Option Explicit

Sub AddComments()

    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    Dim lRow As Long: lRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim lCol As Long: lCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    Dim srg As Range: Set srg = ws.Range("A1").Resize(lRow, lCol)
    Dim Data As Variant: Data = srg.Value
    
    Dim r As Long, c As Long, n As Long
    Dim Comm As String
    
    For c = 2 To lCol
        For r = 4 To lRow
            If Len(Data(r, c)) > 0 Then
                n = n + 1
                Comm = Comm & n & ". " & Data(r, 1) & " - " _
                   & Format(Data(r, c), "$#,##0") & vbLf
            End If
        Next r
        If n > 0 Then
            With srg.Cells(1, c)
                .ClearComments
                .AddComment Left(Comm, Len(Comm) - 1)
            End With
            n = 0
            Comm = ""
        End If
    Next c
    
    MsgBox "Comments added.", vbInformation
    
End Sub

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

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