Consulting

Results 1 to 3 of 3

Thread: Help Required : Highlight a part of text with in a cell (case sensitive problem)

  1. #1
    VBAX Mentor
    Joined
    Nov 2020
    Location
    Cochin, Kerala
    Posts
    314
    Location

    Help Required : Highlight a part of text with in a cell (case sensitive problem)

    Hi, The below code copied from the internet highlights the input keyword but it is subject to case sensitive. For example in the attached file, I have keyword "DOA" but if I input in small letter 'doa', the text is not getting highlighted. Can somebody please help with a solution ?

    Sub HighlightStrings()
    Application.ScreenUpdating = False
    Dim Rng As Range
    Dim cFnd As String
    Dim xTmp As String
    Dim x As Long
    Dim m As Long
    Dim y As Long
    cFnd = InputBox("Enter the text string to highlight")
    y = Len(cFnd)
    For Each Rng In Selection
      With Rng
        m = UBound(Split(Rng.Value, cFnd))
        If m > 0 Then
          xTmp = ""
          For x = 0 To m - 1
            xTmp = xTmp & Split(Rng.Value, cFnd)(x)
            .Characters(Start:=Len(xTmp) + 1, Length:=y).Font.ColorIndex = 3
            xTmp = xTmp & cFnd
          Next
        End If
      End With
    Next Rng
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Option Explicit
    Option Compare Text   ' <<<<<<<<<<<<<<<<<<<<<<<<<
    
    
    Sub HighlightStrings()
        Application.ScreenUpdating = False
        Dim Rng As Range
        Dim cFnd As String
        Dim xTmp As String
        Dim x As Long
        Dim m As Long
        Dim y As Long
        
        cFnd = InputBox("Enter the text string to highlight")
        
        y = Len(cFnd)
        
        For Each Rng In Selection
          With Rng
            m = UBound(Split(Rng.Value, cFnd))
            If m > 0 Then
              xTmp = ""
              For x = 0 To m - 1
                xTmp = xTmp & Split(Rng.Value, cFnd)(x)
                .Characters(Start:=Len(xTmp) + 1, Length:=y).Font.ColorIndex = 3
                xTmp = xTmp & cFnd
              Next
            End If
          End With
        Next Rng
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Mentor
    Joined
    Nov 2020
    Location
    Cochin, Kerala
    Posts
    314
    Location
    Thanks a ton Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •