View Single Post
 
Old 09-18-2023, 01:43 PM
East East is offline Windows 10 Office 2019
Novice
 
Join Date: Aug 2023
Posts: 7
East is on a distinguished road
Default

Try this macro (created by AI):

Code:
Sub ShadeCellsInTable()
    Dim selectedRange As Range
    Dim tbl As Table
    Dim cell As Cell
    Dim findText As String
    Dim alertMessage As String
    
    ' Set the text you want to find
    findText = "Name"
    
    ' Set the alert message
    alertMessage = "Please select a table."
    
    ' Check if a range is selected
    If Selection.Type <> wdSelectionIP Then
        Set selectedRange = Selection.Range
        ' Check if the selection is a table
        If selectedRange.Tables.Count > 0 Then
            Set tbl = selectedRange.Tables(1)
            ' Loop through each cell in the table
            For Each cell In tbl.Range.Cells
                ' Check if the cell contains the find text
                If InStr(cell.Range.Text, findText) > 0 Then
                    ' Shade the cell if it contains the find text
                    cell.Shading.BackgroundPatternColor = wdColorGray25
                End If
            Next cell
        Else
            ' Show an alert if a table is not selected
            MsgBox alertMessage, vbInformation, "Word Macro"
        End If
    Else
        ' Show an alert if a range is not selected
        MsgBox alertMessage, vbInformation, "Word Macro"
    End If
    
End Sub
Reply With Quote