Search code examples
excelvba

how to extract contents from comment to cell in EXCEL VBA?


Sub CommentToCell()
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
    Rng.Value = Rng.NoteText
Next
End Sub

It is my VBA code. but it makes blank if the cell do not get conmment.

For example, I use this function for in the below cell.

1 1 1 0(comments) 1

It becomes

blank blank blank comments blank

I want to get

1 1 1 comments 1

Solution

  • Sub CommentToCell()
    Dim Rng As Range
    Dim WorkRng As Range
    On Error Resume Next
    xTitleId = "KutoolsforExcel"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    For Each Rng In WorkRng
    If Rng.Comment Is Nothing Then
        Rng.Value = Rng.Value
        Else
        Rng.Value = Rng.NoteText
        End If
    Next
    End Sub
    

    The answer was easier than I thought, and I just had to check the IF statement to see if there was a memo. This works well.