繁体   English   中英

使用 For Each 循环向一行中的每个单元格添加注释 (VBA Excel)

[英]Add a comment to every cell in a row with For Each loop (VBA Excel)

我正在尝试使用 For Each 循环向 excel 工作表中第二列的每个单元格添加注释。 我想将单元格地址(例如$B$1)写到评论中。 到目前为止,这是我的代码,但它不起作用。 我不知道出了什么问题。 抱歉,我是菜鸟,不知道如何让循环第二行中的单元格引用起作用:

Sub TestZelladresse()
    
    Dim rng As Range
    Dim cell As Range
    
    Set rng = Columns(2)
    
    For Each cell In rng
    .AddComment (Address(cell))
    Next cell
    
End Sub

正如 BigBen 指出的那样, Address是范围 object 的属性,而不是您将范围传递给的 function。

您不能将For EachColumns范围 object 一起使用,因为它会循环遍历列而不是列中的单元格(再次感谢 BigBen),并且您不能像那样一次性向范围添加注释。 此外,BigBen 还指出,您不想遍历整个列,因为有很多单元格。

最后,您需要将调用方法/属性的 object 包含在该行中,除非您使用With ,它只是同一事物的一些语法糖。

所以总的来说是这样的:

Sub TestZelladresse()
    Dim rng As Range
    Dim cell As Range
    Set rng = Range(Cells(1, 2), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)) 

    For Each cell In rng
        cell.AddComment cell.Address 
    Next cell
    
End Sub

暂无
暂无

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

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