Excel VBA从数组中添加注释

我不明白为什么下面不添加评论从A1到A27 🙁

Dim aComment As String aComment = Split("ABC|DEF|GHI|JKL", "|") For i = 1 To i = 27 For x = LBound(aComment) To UBound(aComment) With Worksheets("report").Range(Cells(1, i)).AddComment .Visible = False .Text aComment(x) End With Next Next 

这将是惊人的,如果你可以给我一只手,知道我差不多在那里,但不能弄清楚我错了:(

如果你想添加来自A1:A4的评论,这是有效的。

 Dim aComment As Variant, i As Long aComment = Split("ABC|DEF|GHI|JKL", "|") For i = 1 To 4 Sheets("report").Range("A" & i).AddComment(aComment(i - 1)).Visible = False 'Sheets("report").Cells(1, i).).AddComment(aComment(i - 1)).Visible = False Next 

至于要改进的东西,使其工作:

  1. 您需要将aComment声明为Variant而不是String
  2. 只需使用一个循环和一个class轮语句。
  3. 而你需要修正你的For Loop SyntaxFor i = 1 To i = 27For i = 1 To 27

我只在循环中使用4 ,因为你只给了四个样本。 你可以调整它适合。
另外我很困惑。 在你的问题你想要把它在A1:A27但在评论A1到D1。
无论我提供两个代码。