Unable To Change Font Color Of A Cell

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code:
Rich (BB code):
Private Sub CommandButton1_Click()
    Stop
    Dim cdd As Range
    Dim firstaddress As String
    
    trid = ws_working.Cells(qrw, 1)
    rval2 = ComboBox1.Value
    openingParen = InStr(rval2, "(")
    closingparen = InStr(rval2, ")")
    encval = Mid(rval2, openingParen + 1, closingparen - openingParen - 1)
    
    cell.Value = encval
    
    cell.Font.Color = vbBlack
    'check if change needs to be made elsewhere...
    cntrpl = 0 'replaced values count
    For Each ws10 In wb_data.Worksheets
        Debug.Print "Worksheet: " & ws10.Name
        If ws10.Visible = xlSheetVisible Then
            cnt5 = Application.WorksheetFunction.CountIf(ws10.Columns(1), trid)
            If cnt5 > 0 Then
                ws10.Activate
                rwmtch = Application.WorksheetFunction.Match(trid, ws10.Columns(1), 0)
                With ws10.Range("H" & rwtch & ":Q" & rwtch)
                    Set cdd = .Find(cval2, LookIn:=xlValues)
                    'Debug.Print cdd.Column
                    If Not cdd Is Nothing Then
                        Debug.Print cdd.Column
                        svc5 = ws10.Cells(10, cdd.Column)
                        If IsNumeric(svc5) = True Then svc5 = "TrnService" & svc5
                        firstaddress = cdd.Address
                        Debug.Print firstaddress
                        Do
                            cdd.Value = Replace(cdd.Value, cval2, encval)
                            Set cdd = .FindNext(cdd)
                            MsgBox cval2 & " was replaced by " & encval & " in " & ws10.Name & " " & firstaddress & " (" & svc5 & ")"
                            'change font color of replacement value here
                        Loop While Not cdd Is Nothing
                    Else
                        Debug.Print cval2 & " not associated to " & trid & " in worksheet: " & ws10.Name
                    End If
                End With
            Else
                Debug.Print "No match of " & trid & " in worksheet: " & ws10.Name
            End If
        Else
            Debug.Print ws10.Name & " excluded [Hidden]"
        End If
    Next ws10
    MsgBox "All visible worksheets checked for instances of " & cval2 & Chr(13) & cntrpl & " replacements were made." & Chr(13) & "(For RID: " & trid & " only.)"
    Unload Me
    'Stop
End Sub

The line in blue is where I would like to add code to change the font color of cell in which the replacement was made. I haven't found a solution.

If I try
Code:
cdd.font.color=vbgreen
I get an 'object required' error.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You are finding a value, then changing it to another value. Eventually you will find and change them all, and FindNext will return Nothing. That is what is causing your error. You need to get out of the loop as soon as cdd is Nothing.

Rich (BB code):
                        Do
                            cdd.Value = Replace(cdd.Value, cval2, encval)
                            Set cdd = .FindNext(cdd)
                            If cdd Is Nothing Then Exit Do
                            MsgBox cval2 & " was replaced by " & encval & " in " & ws10.Name & " " & firstaddress & " (" & svc5 & ")"
                            'change font color of replacement value here
                            cdd.font.color=vbGreen
                       Loop
 
Upvote 0

Forum statistics

Threads
1,215,460
Messages
6,124,949
Members
449,198
Latest member
MhammadishaqKhan

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