简体   繁体   English

VBA动态注释创建

[英]VBA Dynamic Comment Creation

I'm trying to improve my GANTT chart in Excel with VBA. 我正在尝试使用VBA改进Excel中的GANTT图表。 For now, I'm only using conditional formatting, but I need to show the project payment values, dates and status within a comment box which will take its input from three different worksheets inside my workbook: Estudos, Projetos and Obras. 现在,我仅使用条件格式,但是我需要在注释框中显示项目付款的值,日期和状态,该注释框中的内容将来自工作簿中的三个不同工作表:Estudos,Projetos和Obras。

The payment dates are shown as red in the GANTT chart. 付款日期在GANTT图表中显示为红色。 If the payment is in lines 4+3*i, the source is Estudos, if the payment is in lines 5+3*i, the source is Projetos and if its in lines 6+3*i, the source is Obras. 如果付款在第4 + 3 * i行,则源为Estudos,如果付款在第5 + 3 * i行,则源为Projetos,如果付款在6 + 3 * i行,则源为Obras。

Current GANTT chart picture. 当前的甘特图图片。

My idea was to loop between all red cells using three different matrices, one for each worksheet source, but since I'm new in VBA programming, I can't seem to make it work. 我的想法是使用三种不同的矩阵在所有红细胞之间循环,每个工作表源使用一个矩阵,但是由于我是VBA编程的新手,所以我似乎无法使其正常工作。 The syntax and objects are very specific. 语法和对象非常具体。

Please help me! 请帮我!

Estudos worksheet. Estudos工作表。

Above is a picture of the Estudos worksheet from where the comment will take its values. 上面是Estudos工作表的图片,注释将在其中提取其值。 I need to write both the date and value of each payment shown inside its specific red cell located in the GANTT chart. 我需要写在GANTT图表中特定红色单元格内显示的每个付款的日期和值。

This is what I have so far, what it does is it inserts the generic "data" text inside a comment box in each red cell. 到目前为止,这就是我所要做的,是将通用的“数据”文本插入每个红色单元格的注释框中。

            Sub AtualizaComent()

            ' variaveis
            Dim rng1     As Range
            Dim celula   As Range
            Dim estudos  As Range
            Dim projetos As Range
            Dim obras    As Range
            Dim etapa    As String
            Dim data     As String
            Dim valor    As String
            Dim i, j, k, l, m, n As Integer

            ' inicializaçao
            Set rng1 = Range("T4:APV726")
            Set estudos = Worksheets("Operacional - Pag Estudos").Cells(4, 8)
            Set projetos = Worksheets("Operacional - Pag Projetos").Cells(4, 8)
            Set obras = Worksheets("Operacional - Pag Obras").Cells(4, 8)
            i = 0
            j = 0
            k = 0
            l = 0
            m = 0
            n = 0

            ' limpa todos os comentarios
            rng1.ClearComments

            ' para cada celula no gantt
            For Each celula In rng1

                ' valido se a celula for vermelha (data do pagamento)
                If celula.DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
                    ' If celula.Row = 4 + 3 * i Then


                    ' adiciona o comentario
                    With celula.AddComment
                        .Text Text:="data"
                    End With

                    End If
            Next celula

            End Sub

I did it! 我做的! Here's the code I used. 这是我使用的代码。

            Sub AtualizaComent()

            ' variaveis
            Dim gantt    As Range
            Dim linha    As Range
            Dim celula   As Range
            Dim data     As Range
            Dim valor    As Range
            Dim etapa    As Range
            Dim i, j, k, l, m, n As Integer

            ' inicializaçao
            Set gantt = Range("T4:APV726")
            i = 0
            j = 0
            k = 0
            l = 0
            m = 0
            n = 0

            ' limpa todos os comentarios
            gantt.ClearComments

            ' para cada linha no gantt
            For Each linha In gantt.Rows
                If linha.Row = 4 + 3 * i Then
                    ' para cada celula na linha
                    For Each celula In linha.Cells
                        ' valido se a celula for vermelha (data do pagamento)
                        If celula.DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
                            ' celulas que contem as datas, valores e etapa
                            Set data = Worksheets("Operacional - Pag Estudos").Cells(4 + 3 * i, 8 + 2 * j)
                            Set valor = Worksheets("Operacional - Pag Estudos").Cells(5 + 3 * i, 8 + 2 * j)
                            Set etapa = Worksheets("Operacional - Pag Estudos").Cells(6 + 3 * i, 8 + 2 * j)
                            ' adiciona o comentário
                            With celula.AddComment
                                .Text Text:=data.Text & _
                                            Chr(10) & valor.Text & _
                                            Chr(10) & etapa.Text
                            End With
                            j = j + 1
                        End If
                    Next celula
                    i = i + 1
                    j = 0
                End If
            Next linha

            For Each linha In gantt.Rows
                If linha.Row = 5 + 3 * k Then
                    For Each celula In linha.Cells
                        If celula.DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
                            Set data = Worksheets("Operacional - Pag Projetos").Cells(4 + 3 * k, 8 + 2 * l)
                            Set valor = Worksheets("Operacional - Pag Projetos").Cells(5 + 3 * k, 8 + 2 * l)
                            Set etapa = Worksheets("Operacional - Pag Projetos").Cells(6 + 3 * k, 8 + 2 * l)
                            With celula.AddComment
                                .Text Text:=data.Text & _
                                            Chr(10) & valor.Text & _
                                            Chr(10) & etapa.Text
                            End With
                            l = l + 1
                        End If
                    Next celula
                    k = k + 1
                    l = 0
                End If
            Next linha

            For Each linha In gantt.Rows
                If linha.Row = 6 + 3 * m Then
                    For Each celula In linha.Cells
                        If celula.DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
                            Set data = Worksheets("Operacional - Pag Obras").Cells(4 + 3 * m, 8 + 2 * n)
                            Set valor = Worksheets("Operacional - Pag Obras").Cells(5 + 3 * m, 8 + 2 * n)
                            Set etapa = Worksheets("Operacional - Pag Obras").Cells(6 + 3 * m, 8 + 2 * n)
                            With celula.AddComment
                                .Text Text:=data.Text & _
                                            Chr(10) & valor.Text & _
                                            Chr(10) & etapa.Text
                            End With
                            n = n + 1
                        End If
                    Next celula
                    m = m + 1
                    n = 0
                End If
            Next linha

            End Sub

Thank you @Nathan_Sav for the help. 谢谢@Nathan_Sav的帮助。

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

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