Thread: [Solved] Conditional formating table
View Single Post
 
Old 01-18-2018, 05:12 PM
kilroy kilroy is offline Windows 10 Office 2016
Competent Performer
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 118
kilroy is on a distinguished road
Default

Based on the Word document you supplied the following code allows you to choose the text you want to find and choose the column it looks in. I'm sure it could probably be cleaned up. I believe this code was originally written by Greg Maxey but butchered by me to suit your needs.

Code:
Sub BoldRowBasedOnString()
 
Dim strName As String
Dim oDoc As Document
    Dim oTbl As Table
    Dim oCell As Cell
    Dim oRow As Row
    Dim strText As String, strRef As String
    Dim lngCol As Long
    Dim oRng As range
    Set oDoc = ActiveDocument
    Set oTbl = oDoc.Tables(2)
    strText = InputBox("Search text?")
    lngCol = CLng(InputBox("Enter column to search", "Must be 1 - 5"))
    For Each oCell In oTbl.range.Cells
    If oCell.RowIndex > 1 And oCell.ColumnIndex = lngCol Then
            On Error Resume Next
            If Left(oTbl.Cell(oCell.RowIndex, 1).range.Text, Len(oTbl.Cell(oCell.RowIndex, 1).range.Text) - 2) <> vbNullString Then
                strRef = Left(oTbl.Cell(oCell.RowIndex, 1).range.Text, Len(oTbl.Cell(oCell.RowIndex, 1).range.Text) - 2)
            End If
            If Left(oTbl.Cell(oCell.RowIndex, 1).range.Text, Len(oTbl.Cell(oCell.RowIndex, 1).range.Text) - 2) = vbNullString Then
                oTbl.Cell(oCell.RowIndex, 1).range.Text = strRef
            End If
            On Error GoTo 0
            If InStr(UCase(oCell.range.Text), UCase(strText)) > 0 Then
               oCell.range.Select
               With Selection
                  Selection.SelectRow
                  Selection.BoldRun
               End With
            End If
    End If
    Next
lbl_Exit:
    Set oDoc = Nothing: Set oTbl = Nothing: Set oCell = Nothing
    Exit Sub
    End Sub
Reply With Quote