VBA를 통해 Excel 셀에 동적 주석 추가

VBA를 통해 Excel 셀에 동적 주석 추가

VBA 매크로를 통해 작업의 현재 기한을 표시하기 위해 주석을 사용하려고 합니다. 내 현재 솔루션은 다음과 같습니다.

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

나는 이것이 엉성한 코드일 가능성이 높다는 것을 알고 있지만 이제 막 다시 시작하고 있습니다.

지금까지의 솔루션은 단일 셀에 대해 잘 작동합니다. "B12"와 같은 셀 참조를 사용하는 대신 미리 셀 이름을 "Target" 및 "Source"로 지정했습니다. 이제 미리 선택한 범위(예: A1:A6)에 따라 여러 셀로 확장하고 싶습니다.

설명이 추가될 선택 항목은 다른 워크시트의 동일한 크기 범위에 해당합니다.

루프가 도움이 될 것 같지만 어디서부터 시작해야 할지 모르겠습니다.

아래 그림은 내가 하고 싶은 일을 설명할 수 있습니다. 소스는 내 댓글에 추가하고 싶은 동적 날짜로 채워져 있습니다.

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

미리 감사드립니다

답변1

이제 시작해야 합니다. 이는 샘플 사진에서 작동하지만 설명 소스가 다른 워크시트인 경우 조정이 필요합니다.

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

여기에 이미지 설명을 입력하세요

관련 정보