簡體   English   中英

Excel VBA 如果其他列中的單元格包含值,則向單元格添加注釋

[英]Excel VBA Add comment to cell if cell in other column contains value

我有一個代碼,它結合了 C:F 列中所有單元格的單元格內容,並將其放入 B 列的注釋中 - 每行。 我現在只需要將其應用於在各自的列 A 中有內容的行。

單元格 A2 里面有東西,所以把 C2:F2 的內容放到 B2 的注釋中。 單元格 A3 中沒有任何內容,因此不要向該單元格添加注釋。 單元格A4又有東西了,所以把C4:F4的內容放到B4的注釋里。

該表看起來像這樣:

到目前為止,我的代碼如下所示:

Sub Test()
    Dim LRow As Integer
    
    With ActiveSheet
        For LRow = 2 To Range("A" & Rows.Count).End(xlUp).Row
        Range(Cells(LRow, 3), Cells(LRow, 6)).Select
        
        Dim c As Range, s As String

        With Cells(LRow, 2)
            .ClearComments
            For Each c In Selection
            'If c.Offset(0, -2) <> "" Then
                'On Error Resume Next
                If c <> "" Then s = IIf(s = "", c, s & Chr(10) & c)
                Next c
                .AddCommentThreaded "Test:" & Chr(10) & s
        End With
        s = ""
        Next LRow
    End With
End Sub

現在的問題是我無法在 A 列中進行內容檢查。 有人對如何使該位起作用有任何提示嗎?

試試下面的東西。 請查看如何避免 select以及為什么使用 long 而不是 integer

Sub Test()
    Dim LRow As Long, aCell As Range, ws As Worksheet
    Set ws = ActiveSheet
    
    With ws
    
        For LRow = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            If .Cells(LRow, 1).Value <> "" Then
                Dim theComment As String
                theComment = ""
                
                For Each aCell In Intersect(Range("C:F"), .Rows(LRow)).Cells
                    theComment = theComment & aCell.Value
                Next aCell
    
            With .Cells(LRow, 2)
                .ClearComments
                    .AddCommentThreaded "Test:" & Chr(10) & theComment
            End With
            End If
            
        Next LRow
    End With
End Sub

暫無
暫無

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

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