Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-22-2024, 03:06 PM
Kater Kater is offline VBA to highlight certain text in a table Windows 10 VBA to highlight certain text in a table Office 2016
Novice
VBA to highlight certain text in a table
 
Join Date: Apr 2024
Posts: 2
Kater is on a distinguished road
Default VBA to highlight certain text in a table

Hello! I have documents that often contain tables where the first column says "NAME:" and then the corresponding row in the second column is where that person would enter their name.



I would like a macro that would highlight the column 2 text (the person's name) IF it does not begin with "AGENT." For example, column 1 says "NAME:", column 2 says "BOB JONES" but it should say "AGENT BOB JONES."

I would prefer the macro to highlight the text (any color is fine) rather than shade the cell.

Also, the macro would need to just end if there happen to not be any tables in said document.

Is that possible?
Reply With Quote
  #2  
Old 04-22-2024, 08:26 PM
syl3786 syl3786 is offline VBA to highlight certain text in a table Windows 10 VBA to highlight certain text in a table Office 2019
Advanced Beginner
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by Kater View Post
Hello! I have documents that often contain tables where the first column says "NAME:" and then the corresponding row in the second column is where that person would enter their name.

I would like a macro that would highlight the column 2 text (the person's name) IF it does not begin with "AGENT." For example, column 1 says "NAME:", column 2 says "BOB JONES" but it should say "AGENT BOB JONES."

I would prefer the macro to highlight the text (any color is fine) rather than shade the cell.

Also, the macro would need to just end if there happen to not be any tables in said document.

Is that possible?
Code:
Sub HighlightNames()
    Dim oTable As Table
    Dim oRow As Row
    Dim oRange As Range

    If ActiveDocument.Tables.Count = 0 Then Exit Sub

    For Each oTable In ActiveDocument.Tables
        For Each oRow In oTable.Rows
            Set oRange = oRow.Cells(2).Range
            oRange.End = oRange.End - 1
            
            If Not oRange.Text Like "AGENT*" Then
                oRange.HighlightColorIndex = wdYellow
            End If
        Next oRow
    Next oTable
End Sub
Reply With Quote
  #3  
Old 04-23-2024, 07:07 AM
Kater Kater is offline VBA to highlight certain text in a table Windows 10 VBA to highlight certain text in a table Office 2016
Novice
VBA to highlight certain text in a table
 
Join Date: Apr 2024
Posts: 2
Kater is on a distinguished road
Default

Thank you so much for your response! It worked great except that I changed one section because there were some rows in the table that were merged (so they only had one cell in them):

For Each oTable In ActiveDocument.Tables
For Each oRow In oTable.Rows
If oRow.Cells.Count = 2 Then
Set oRange = oRow.Cells(2).range
oRange.End = oRange.End - 1
End If
Reply With Quote
Reply

Tags
highlight color, table



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to highlight text, but not all text, in Outlook 2016, 365? cricket1001 Outlook 2 08-12-2020 11:21 AM
VBA to highlight certain text in a table Macro to highlight alternating sections of a table Jennifer Murphy Word VBA 35 02-23-2020 02:53 PM
Highlight a Cell which is in a ContentControl which is in a table keine Word VBA 1 01-02-2020 08:35 AM
like to highlight web text, enter cntl key, have text appended to same onenote pate bobk544 OneNote 0 12-10-2017 11:49 AM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft