【问题标题】:VBA Dynamic Comment CreationVBA 动态注释创建
【发布时间】:2019-05-06 04:04:59
【问题描述】:

我正在尝试使用 VBA 在 Excel 中改进我的甘特图。目前,我只使用条件格式,但我需要在评论框中显示项目付款值、日期和状态,该评论框将从我的工作簿中的三个不同工作表中获取输入:Estudos、Projetos 和 Obras。

付款日期在甘特图中显示为红色。如果付款在第 4+3*i 行,则来源为 Estudos,如果付款在第 5+3*i 行,则来源为 Projetos,如果付款在第 6+3*i 行,则来源为 Obras。

Current GANTT chart picture.

我的想法是使用三个不同的矩阵在所有红色单元格之间循环,每个工作表源一个,但由于我是 VBA 编程的新手,我似乎无法让它工作。语法和对象非常具体。

请帮帮我!

Estudos worksheet.

上面是 Estudos 工作表的图片,评论将从中获取其值。我需要在位于甘特图中的特定红格内写下每笔付款的日期和金额。

这是我目前所拥有的,它的作用是将通用“数据”文本插入到每个红色单元格的注释框中。

            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

【问题讨论】:

  • 如果值在单元格中,为什么不使用查找而不是代码?
  • 他们必须在单元格内的评论中!单元格太小,无法显示所有这些值。你看?如图1所示,小红细胞。
  • 啊,一个单元格在 Excel 论坛中意味着其他东西 :) 看看在你的循环中使用沿着这些线的东西,activecell.AddComment(cstr(application.WorksheetFunction.VLookup(range("a1").Value,range("g1:h100"),2,false)))
  • 我已经添加了到目前为止的代码!那么我会尝试使用查找。谢谢老兄!
  • 另外,看看使用按格式查找来找到你的红细胞

标签: excel vba conditional comments gantt-chart


【解决方案1】:

我做到了!这是我使用的代码。

            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

感谢@Nathan_Sav 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多