簡體   English   中英

將多個單元格值插入評論

[英]Insert multiple cell values to a comment

我的任務是按月在總計中添加評論以顯示所涉及的項目和金額。 我想簡化工作,因為我按月和每個工作(超過 20 個工作)來做!

我找到了單個細胞的解決方案。

我需要將相關的單元格值添加到該月的一條評論中。
在此處輸入圖像描述

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

向范圍添加注釋

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