View Single Post
 
Old 01-03-2016, 11:40 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Based on your code example, with the cursor in the table run the following macro.
It was not clear whether there was already a value in column 3. The macro writes the FirstName value after any text in the cell with a space between.
Code:
Sub ReplaceInTable()
Dim FirstName As String
Dim LastName As String
Dim oRng As Range
Dim oCell As Range
Dim oTable As Table
Dim oRow As Row
Dim bText As Boolean
Dim i As Long
    Set oTable = Selection.Tables(1)
    FirstName = InputBox("Type text", "")
    LastName = InputBox("Search BD No", "")
    For i = 1 To oTable.Rows.Count
        bText = False
        Set oRow = oTable.Rows(i)
        Set oRng = oRow.Cells(1).Range
        If InStr(1, oRng.Text, LastName) > 0 Then
            Set oCell = oRow.Cells(3).Range
            oCell.End = oCell.End - 1
            If Len(oCell) > 0 Then bText = True
            oCell.Collapse 0
            If bText = True Then
                oCell.Text = Chr(32) & FirstName
            Else
                oCell.Text = FirstName
            End If
        End If
    Next i
lbl_Exit:
    Set oTable = Nothing
    Set oRow = Nothing
    Set oCell = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote