VBA 循环评论

VBA Comments In a Loop

我包含了整个 header 代码只是为了让您了解我有多少变量以及我在这方面有多糟糕。目前代码效果很好!尝试在复制阶段添加评论。此代码不会保留大量数据,因为它会严重拖慢它。一旦引用循环,除非我制作一个新的整个页面来挖掘它,否则就无法恢复它。而 Vlookup 会遍历许多单元格才能生效。

这需要一系列事件在日历上用 1 标记到期日。日历 运行 28 天,然后下降到下个月开始(准确地说是 28 天) ) 我可以获得对第一行中的任何内容起作用的注释,但是当它试图跳转到第二个月时,它会出错,说未定义的变量。任何帮助,将不胜感激。

 Sub Planner()
 Dim data As Worksheet
 Dim Cal As Worksheet
 Dim C1, C2, C3, C4, C5, C6, C7, C8, C9 As Worksheet

 Dim x1C, x2C, x3C, x4C, x5C, x6C, x7C, x8C, x9C As Integer
 Dim y1C, y2C, y3C, y4C, y5C, y6C, y7C, y8C, y9C  As Integer
 Dim Cal1C, Cal2C, Cal3C, Cal4C, Cal5C, Cal6C, Cal7C, Cal8C, Cal9C As Integer
 Dim a1C, a2C, a3C, a4C, a5C, a6C, a7C, a8C, a9C As Integer
 Dim b1C, b2C, b3C, b4C, b5C, b6C, b7C, b8C, b9C As Integer
 Dim c1c, c2c, c3c, c4c, c5c, c6c, c7c, c8c, c9c As Integer


 Dim Com1C, Com2C, Com3C, Com4C, Com5C, Com6C, Com7C, Com8C, Com9C As Integer
 Dim Dest1C As Range
 Dim MDLDate As Integer


 Set data = ThisWorkbook.Sheets("Data")
 Set Cal = ThisWorkbook.Sheets("Planner")
 Set C1 = ThisWorkbook.Sheets("C1")
 Set C2 = ThisWorkbook.Sheets("C2")
 Set C3 = ThisWorkbook.Sheets("C3")
 Set C4 = ThisWorkbook.Sheets("C4")
 Set C5 = ThisWorkbook.Sheets("C5")
 Set C6 = ThisWorkbook.Sheets("C6")
 Set C7 = ThisWorkbook.Sheets("C7")
 Set C8 = ThisWorkbook.Sheets("C8")
 Set C9 = ThisWorkbook.Sheets("C9")


 Cal1C = C1.Range("A500").End(xlUp).Row

 'sets date MDL was pulled for C2 puts on data page
 data.Cells(1, 7) = C1.Cells(Cal1C, 12).Value - C2.Cells(Cal1C, 13)


 For x1C = 2 To Cal1C 'x = rows y = columns
 For y1C = 50 To 50
    If C1.Cells(x1C, 13) < 1 Then
        data.Cells(18, 8) = "X"
            GoTo next1C
    Else: c1c = C1.Cells(x1C, 13)
        End If

         a1C = Int(c1c / 28) * 24 ' gets the interval of the reference for the column address
         aa1c = a1C + 8 'takes the cell address for column and offsets it to fit cal
         b1C = c1c Mod 28 'get the remainder of the reference for row address
         bb1c = b1C + 3 'takes the cell address for row and offsets it to fit cal
         Set Dest1C = Cal.Cells(aa1c, bb1c) 'tried to set the cell output as a reference but all it equals is 1 not the address. i suspect this is part of my problem. 
         Com1C = C1.Cells(x1C, 8) ' this is text i want in the comment
         Cal.Cells(aa1c, bb1c).Value = 1 ' this is what actually places the number on the calendar based on the cell reference. with the exception of the 1 this is where i want the comments Comm1C.

         With Dest1C
             .AddComment
             .Comment.Text Text:=Com1C
         End With

         Next y1C
 next1C:
     Next x1C

首先尝试检查 .Comment,如果不存在,则仅检查 .AddComment。用新文本附加当前评论。

     With Dest1C
         If .Comment Is Nothing Then .AddComment
         .Comment.Text Text:=.Comment.Text & Com1C & vbLf
     End With