VBA - add comment to cell that script highlights

mike760534211

New Member
Joined
Sep 25, 2014
Messages
13
I have a code that highlights the cell if the cell value matches the array and it is working perfectly.
I was wanting it to also add a comment to the cell it locates from the array and after much searching for this I am unable to get it to add the comment correctly to the cell it found from the array.
Any assistance would be greatly appreciated.

Code:
Sub Highlight_Cell_frm_Array()

Dim vntWords As Variant
Dim lngIndex As Long
Dim rngFind As Range
Dim strFirstAddress As String
Dim lngPos As Long


vntWords = Array("array criteria here")

With ActiveSheet.UsedRange
    For lngIndex = LBound(vntWords) To UBound(vntWords)
        Set rngFind = .Find(vntWords(lngIndex), LookIn:=xlValues, LookAt:=xlPart)
        If Not rngFind Is Nothing Then
            strFirstAddress = rngFind.Address
            Do
                lngPos = 0
                Do
                    lngPos = InStr(lngPos + 1, rngFind.Value, vntWords(lngIndex), vbTextCompare)
                    If lngPos > 0 Then
                        With rngFind.Characters(lngPos, Len(vntWords(lngIndex)))
                            .Font.Bold = True
                            .Font.Size = .Font.Size + 2
                            .Font.ColorIndex = 3
                        End With
                    End If
                Loop While lngPos > 0
                Set rngFind = .FindNext(rngFind)
            Loop While rngFind.Address <> strFirstAddress
        End If
    Next
End With
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Mike, try...

Code:
Sub Highlight_Cell_frm_Array()

Dim vntWords As Variant
Dim lngIndex As Long
Dim rngFind As Range
Dim strFirstAddress As String
Dim lngPos As Long




vntWords = Array("array criteria here")


With ActiveSheet.UsedRange
    For lngIndex = LBound(vntWords) To UBound(vntWords)
        Set rngFind = .Find(vntWords(lngIndex), LookIn:=xlValues, LookAt:=xlPart)
        If Not rngFind Is Nothing Then
[COLOR=#ff0000]        rngFind.AddComment ("Add Your Comment Here!")[/COLOR]
        
            strFirstAddress = rngFind.Address
            Do
                lngPos = 0
                Do
                    lngPos = InStr(lngPos + 1, rngFind.Value, vntWords(lngIndex), vbTextCompare)
                    If lngPos > 0 Then
                        With rngFind.Characters(lngPos, Len(vntWords(lngIndex)))
                            .Font.Bold = True
                            .Font.Size = .Font.Size + 2
                            .Font.ColorIndex = 3
                        End With
                    End If
                Loop While lngPos > 0
                Set rngFind = .FindNext(rngFind)
            Loop While rngFind.Address <> strFirstAddress
        End If
    Next
End With
End Sub

Hope that helps.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top