简体   繁体   中英

VBA Dynamic Comment Creation

I'm trying to improve my GANTT chart in Excel with VBA. 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.

The payment dates are shown as red in the GANTT chart. 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.

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. The syntax and objects are very specific.

Please help me!

Estudos worksheet.

Above is a picture of the Estudos worksheet from where the comment will take its values. I need to write both the date and value of each payment shown inside its specific red cell located in the GANTT chart.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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