Using a macro to highlight a case sensitive text

hnt007

Board Regular
Joined
Dec 18, 2021
Messages
98
Office Version
  1. 365
Platform
  1. MacOS
Hello and many thanks for helping me out!

I'd need some help to update the following code to make it case sensitive, please. I just want to highlight "Sec" not "sec"

Sub Sec()
Dim rng As Range, c As Range, i As Long, mystring As String, tmp As String
Application.ScreenUpdating = False
Set rng = Range("D8:D1000")
mystring = "Sec"

For Each c In rng
For i = 1 To Len(c)
tmp = Mid(c, i, 1)
If InStr(1, mystring, tmp, 0) Then c.Characters(i, 1).Font.Color = vbRed
Next i
Next c
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try it like this...
VBA Code:
Sub Sec()
  Dim Pos As Long, rng As Range, c As Range, mystring As String
  Application.ScreenUpdating = False
  Set rng = Range("D8:D1000")
  mystring = "Sec"
  
  For Each c In rng
    Pos = InStr(c.Value, mystring)
    Do While Pos
      c.Characters(Pos, Len(mystring)).Font.Color = vbRed
      Pos = InStr(Pos + 1, c.Value, mystring)
    Loop
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
VBA Code:
Option Explicit
Sub Sec()
Dim lr&, i&, cell As Range, mystring As String
mystring = "Sec"
lr = Cells(Rows.Count, "D").End(xlUp).Row
    For Each cell In Range("D1:D" & lr)
            For i = 1 To Len(cell)
                With cell.Characters(i, 1)
                    If mystring Like "*" & .Text & "*" Then
                        .Font.Color = vbRed
                    End If
                End With
            Next
    Next
End Sub
 
Upvote 0
VBA Code:
Option Explicit
Sub Highlight_Sec()
    Dim rng As Range, c As Range, myString As String, i As Long
    Set rng = Range("D8:D1000")
    myString = "Sec"
    
        For Each c In rng
            For i = 1 To Len(c) - Len(myString) + 1
                If Mid(c, i, Len(myString)) = myString Then
                    c.Characters(i, Len(myString)).Font.Color = vbRed
                End If
            Next i
        Next c
End Sub
 
Upvote 0
Try it like this...
VBA Code:
Sub Sec()
  Dim Pos As Long, rng As Range, c As Range, mystring As String
  Application.ScreenUpdating = False
  Set rng = Range("D8:D1000")
  mystring = "Sec"
 
  For Each c In rng
    Pos = InStr(c.Value, mystring)
    Do While Pos
      c.Characters(Pos, Len(mystring)).Font.Color = vbRed
      Pos = InStr(Pos + 1, c.Value, mystring)
    Loop
  Next
  Application.ScreenUpdating = True
End Sub
This is working great!! Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,854
Messages
6,121,941
Members
449,056
Latest member
denissimo

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