Consulting

Results 1 to 12 of 12

Thread: Solved: Highlighting a specific Text in excel sheet by macro

  1. #1
    VBAX Regular
    Joined
    Nov 2011
    Posts
    34
    Location

    Solved: Highlighting a specific Text in excel sheet by macro

    Hi, I would like to highlight particular TEXT in the cell to be in BOLD and in Color.

    Please let me know , how can i get this by Macro.


    Regards,
    NM

  2. #2
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Try
    [vba]
    Sub BoldandColor_Part_of_String()
    Application.ScreenUpdating = False

    With Range("A1")

    .Characters(Start:=4, Length:=6).Font.Bold = True

    .Characters(Start:=4, Length:=6).Font.ColorIndex = 3

    End With
    Application.ScreenUpdating = True
    End Sub
    [/vba]
    Or does the code need to search for a word, then color and bold it? As that will be different. Above my head a little.

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi and wellcome to VBAX.

    record a macro and see what comes...

    assumuming string to be highlighted is in ActiveCell
    [VBA]
    With ActiveCell.Characters(Start:=9, Length:=5).Font
    .Color = vbRed
    End With
    [/VBA]

    starting character and the length of characters must be specified.
    you may wish to find and assign to variables. if so, you can use InStr, InStrRev, Len, etc functions.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Regular
    Joined
    Nov 2011
    Posts
    34
    Location
    Hi All,
    Thanks for your response.
    My requirement is, I have two specific words "Now :" and "Was :" in the sheet, which explains about the two versions changes.I want to highlight those two words in the excel sheet which will be clearly visible. I would like to share the code which i use for comparion of 2 version of sheets.

    Sub test() 
        Dim a, i As Long, ii As Long, w(), temp, flg As Boolean 
        With Workbooks.Open(ThisWorkbook.Path & "\LMSws1.xls") 
            a = .Sheets(1).Range("a1").CurrentRegion.Value 
            .Close False 
        End With 
        Redim w(1 To UBound(a, 2)) 
        With CreateObject("Scripting.Dictionary") 
            .CompareMode = 1 
            For i = 2 To UBound(a, 1) 
                For ii = 1 To UBound(a, 2) 
                    w(ii) = a(i, ii) 
                Next 
                .Item(a(i, 1)) = w 
            Next 
            a = ThisWorkbook.Sheets(1).Range("a1").CurrentRegion.Value 
            For i = 2 To UBound(a, 1) 
                If .exists(a(i, 1)) Then 
                    w = .Item(a(i, 1)) 
                    For ii = 2 To UBound(a, 2) 
                        If w(ii) <> a(i, ii) Then 
                            temp = a(i, ii): a(i, ii) = "" 
                            a(i, ii) = "Was : " & w(ii) & vbLf & _ 
                            "Now : " & temp 
                            flg = True 
                        End If 
                    Next 
                    If Not flg Then 
                        a(i, 1) = "" 
                        flg = False 
                    End If 
                End If 
            Next 
        End With 
        With ThisWorkbook.Sheets(2) 
            With .Range("a1").Resize(UBound(a, 1), UBound(a, 2)) 
                .Value = a 
                On Error Resume Next 
                .Columns(1).SpecialCells(4).EntireRow.Delete 
                On Error Goto 0 
            End With 
        End With 
         
    End Sub
    I hope this clears you my requirements.

    Regards,
    NM

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    perhaps..

    [vba]
    Dim strHL
    Dim Cll As Range, rng As Range
    Dim j As Integer

    With ThisWorkbook.Sheets(2)
    Set rng = .Range("A1").Resize(UBound(a, 1), UBound(a, 2))
    rng.Value = a
    strHL = Array("Now :", "Was :")
    For j = LBound(strHL) To UBound(strHL)
    For Each Cll In rng
    If InStr(Cll, strHL(j)) <> 0 Then
    Cll.Characters(, 5).Font.Color = vbRed
    'Cll.Characters(, 3).Font.Color = vbRed '" :" exluded
    End If
    Next Cll
    Next j
    On Error Resume Next
    .Columns(1).SpecialCells(4).EntireRow.Delete
    On Error GoTo 0
    End With

    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Regular
    Joined
    Nov 2011
    Posts
    34
    Location
    Hi,
    the above code throwing error"Type Mismatch" while running separtely.
    Set rng = .Range("A1").Resize(UBound(a, 1), UBound(a, 2))
    rng.Value = a

    But i run the above code along with the code i provided, then it is highlighting only the "Was :" text. the "Now :" text remains the same.can you please let me know on this.

    Appreciate your help.

    Regards,
    NM
    Last edited by NM123; 11-15-2011 at 01:50 AM.

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    if you want to call the sub from within another procedure, try.

    [vba]
    Sub MacroTest()
    Dim strHL
    Dim Cll As Range, rng As Range
    Dim j As Integer
    With ThisWorkbook.Sheets(1)
    Set rng = .Range("A9").Resize(20, 10) 'sample range
    strHL = Array("Now :", "Was :")
    For j = LBound(strHL) To UBound(strHL)
    For Each Cll In rng
    If InStr(Cll, strHL(j)) <> 0 Then
    Cll.Characters(, 5).Font.Color = vbRed
    End If
    Next Cll
    Next j
    End With
    End Sub
    [/vba]

    this code worked for me...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    VBAX Regular
    Joined
    Nov 2011
    Posts
    34
    Location
    Hi Sir,
    I wondering if it is working for you then why not for me !!! i am really struggling.
    I have added the code with my compare code. Please do have a look and let me know where i am being wrong. Its really getting tough learning for me.

     
    Sub test()
    Dim a, i As Long, ii As Long, w(), temp, flg As Boolean
    With Workbooks.Open(ThisWorkbook.Path & "\WMS - Requirements_06-28-11.xls")
    a = .Sheets(1).Range("a1").CurrentRegion.Value
    .Close False
    End With
    ReDim w(1 To UBound(a, 2))
    With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    For i = 2 To UBound(a, 1)
    For ii = 1 To UBound(a, 2)
    w(ii) = a(i, ii)
    Next
    .Item(a(i, 1)) = w
    Next
    a = ThisWorkbook.Sheets(1).Range("a1").CurrentRegion.Value
    For i = 2 To UBound(a, 1)
    If .exists(a(i, 1)) Then
    w = .Item(a(i, 1))
    For ii = 2 To UBound(a, 2)
    If w(ii) <> a(i, ii) Then
    temp = a(i, ii): a(i, ii) = ""
    a(i, ii) = "Was : " & w(ii) & vbLf & _
    "Now : " & temp
    flg = True
    End If
    Next
    If Not flg Then
    a(i, 1) = ""
    flg = False
    End If
    End If
    Next
    End With
    With ThisWorkbook.Sheets(2)
    With .Range("a1").Resize(UBound(a, 1), UBound(a, 2))
    .Value = a
    On Error Resume Next
    .Columns(1).SpecialCells(4).EntireRow.Delete
    On Error GoTo 0
    End With
    End With
    Dim strHL
    Dim Cll As Range, rng As Range
    Dim j As Integer
    With ThisWorkbook.Sheets(1)
    Set rng = .Range("A9").Resize(20, 10) 'sample range
    strHL = Array("Now :", "Was :")
    For j = LBound(strHL) To UBound(strHL)
    For Each Cll In rng
    If InStr(Cll, strHL(j)) <> 0 Then
    Cll.Characters(, 5).Font.Color = vbRed
    End If
    Next Cll
    Next j
    End With
    End Sub

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    [vba]Set rng = .Range("A9").Resize(20, 10) 'sample range
    [/vba]

    this was for representing purposes only. change it to actual range where "Was :"s and "Now :"s are present, which i believe would be defined by:
    [vba]Set rng = .Range("a1").Resize(UBound(a, 1), UBound(a, 2))
    [/vba]

    or if you can upload your file, will do it for you
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  10. #10
    VBAX Regular
    Joined
    Nov 2011
    Posts
    34
    Location
    Sir,
    I have attached the sheet. Please do have a look.

    Thanking You,
    NM
    Attached Files Attached Files

  11. #11
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    it seems i also need "WMS - Requirements_06-28-11.xls" file to determine the size of the range into which array elements are pasted.

    but never mind.
    i added variables to do that.

    that code provided makes only "Was :"s highlighted. because you did not mention that they were in the same cells. maybe that point is clear from entirety of your code but i did not go through all your code.

    below may give you what you need.

    [vba]
    Sub test()

    Dim a, w(), temp, strHL
    Dim flg As Boolean
    Dim cll As Range, rng As Range
    Dim i As Long, ii As Long, j As Long
    Dim LastRow As Long, LastCol As Long, SelStart As Long
    Dim flg As Boolean

    With Workbooks.Open(ThisWorkbook.Path & "\LMSws1.xls")
    a = .Sheets(1).Range("a1").CurrentRegion.Value
    .Close False
    End With

    ReDim w(1 To UBound(a, 2))

    With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    For i = 2 To UBound(a, 1)
    For ii = 1 To UBound(a, 2)
    w(ii) = a(i, ii)
    Next
    .Item(a(i, 1)) = w
    Next
    a = ThisWorkbook.Sheets(1).Range("a1").CurrentRegion.Value
    For i = 2 To UBound(a, 1)
    If .exists(a(i, 1)) Then
    w = .Item(a(i, 1))
    For ii = 2 To UBound(a, 2)
    If w(ii) <> a(i, ii) Then
    temp = a(i, ii): a(i, ii) = ""
    a(i, ii) = "Was : " & w(ii) & vbLf & _
    "Now : " & temp
    flg = True
    End If
    Next
    If Not flg Then
    a(i, 1) = ""
    flg = False
    End If
    End If
    Next
    End With

    With ThisWorkbook.Sheets(2)
    With .Range("a1").Resize(UBound(a, 1), UBound(a, 2))
    .Value = a
    On Error Resume Next
    .Columns(1).SpecialCells(4).EntireRow.Delete
    On Error GoTo 0
    End With

    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    Set rng = .Range(.Cells(1, 1), .Cells(LastRow, LastCol))
    strHL = Array("Now :", "Was :")
    For j = LBound(strHL) To UBound(strHL)
    For Each cll In rng
    If InStr(cll, strHL(j)) <> 0 Then
    SelStart = InStr(cll, strHL(j))
    cll.Characters(SelStart, 5).Font.Color = vbRed
    End If
    Next cll
    Next j
    End With

    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  12. #12
    VBAX Regular
    Joined
    Nov 2011
    Posts
    34
    Location
    Thanks a lot for the Information Sir !!!

Posting Permissions

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