Agregue comentarios dinámicos a celdas de Excel a través de VBA

Agregue comentarios dinámicos a celdas de Excel a través de VBA

Estoy intentando utilizar comentarios para mostrar la fecha de vencimiento actual de una tarea a través de una macro de VBA. Mi solución actual se ve así:

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

Soy muy consciente de que probablemente se trate de un código descuidado, pero recién estoy empezando de nuevo.

La solución hasta ahora funciona bien para una sola celda. Llamé a las celdas "Destino" y "Fuente" de antemano en lugar de usar referencias de celda como "B12". Ahora quiero extender eso a varias celdas, dependiendo del rango que seleccioné de antemano (por ejemplo, A1:A6).

La selección donde se agregarán los comentarios corresponderá a un rango de igual tamaño en una hoja de trabajo diferente.

Siento que un bucle será útil, pero no sé por dónde empezar.

La siguiente imagen podría ilustrar lo que quiero hacer. La fuente está llena de fechas dinámicas que quiero agregar a mis comentarios.

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

gracias de antemano

Respuesta1

Esto debería ayudarte a empezar. Esto funciona con su foto de muestra, pero será necesario modificarlo si la Fuente del comentario es una hoja de trabajo 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

ingrese la descripción de la imagen aquí

información relacionada