Adicione comentários dinâmicos às células do Excel via VBA

Adicione comentários dinâmicos às células do Excel via VBA

Estou tentando usar comentários para mostrar a data de vencimento atual de uma tarefa por meio de uma macro VBA. Minha solução atual é assim:

Sub AddDueDates()
Dim strPrefix As String
  strPrefix = ""
  With Range("Target")
If .Comment Is Nothing Then
   .AddComment
End If
.Comment.Visible = True
.Comment.Shape.TextFrame.AutoSize = True
  End With

 With Range("Target").Comment
.Text strPrefix & Range("Source").Text 
End With
End Sub

Estou muito ciente de que este provavelmente é um código desleixado, mas estou apenas começando de novo.

A solução até agora funciona bem para uma única célula. Nomeei as células como "Destino" e "Fonte" antecipadamente, em vez de usar referências de células como "B12". Agora quero estender isso para várias células, dependendo do intervalo selecionado anteriormente (por exemplo, A1:A6).

A seleção onde serão adicionados comentários corresponderá a um intervalo de igual tamanho em uma planilha diferente.

Sinto que um loop será útil, mas não sei por onde começar.

A imagem abaixo pode ilustrar o que eu quero fazer. A fonte está preenchida com datas dinâmicas que desejo adicionar aos meus comentários

https://i.stack.imgur.com/EsfEa.jpg

desde já, obrigado

Responder1

Isso deve começar. Isso funciona com sua foto de amostra - mas precisará de ajustes se a fonte do comentário for uma planilha diferente.

Sub AddDates()
Dim targetRng As Range, commentSrcRng As Range
Dim strPrefix As String ' why this variable? You never seem to use it

Set targetRng = Application.InputBox("Please select the target range.  This is the range that will have comments added to each cell", Type:=8)
Set commentSrcRng = targetRng.Offset(0, 3) ' from the screenshot. Will have to tweak if this is a different worksheet.

Dim cel As Range
Dim i As Long
i = 1
For Each cel In targetRng
    If cel.Comment Is Nothing Then
        cel.AddComment
    End If
    cel.Comment.Visible = True
    cel.Comment.Shape.TextFrame.AutoSize = True
    cel.Comment.Text strPrefix & commentSrcRng.Cells(i)
    i = i + 1
Next cel

End Sub

insira a descrição da imagem aqui

informação relacionada