Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-03-2016, 08:31 AM
auditio auditio is offline Search in first column in table Windows 7 32bit Search in first column in table Office 2013
Novice
Search in first column in table
 
Join Date: Jan 2016
Posts: 3
auditio is on a distinguished road
Default Search in first column in table

I have many table in a document. I want to search input data (stored in variable with input box) in first column of all the table and paste a variable data (stored in variable with input box) in the third column after text in the same row. I want to do the search & input data for multiple text one after another as many as.


My trying code

Dim FirstName As String
FirstName = InputBox("Type text", "")
Dim LastName As String
LastName = InputBox("Search BD No", "")
Selection.Find.ClearFormatting
With Selection.Find
.Text = LastName
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute


Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=FirstName
End Sub
Reply With Quote
  #2  
Old 01-03-2016, 11:40 PM
gmayor's Avatar
gmayor gmayor is offline Search in first column in table Windows 10 Search in first column in table Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 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
  #3  
Old 01-04-2016, 08:16 AM
auditio auditio is offline Search in first column in table Windows 7 32bit Search in first column in table Office 2013
Novice
Search in first column in table
 
Join Date: Jan 2016
Posts: 3
auditio is on a distinguished road
Default

Thanks Gmayor, I want to search in all the table in the document. and search one after another.
Reply With Quote
  #4  
Old 01-04-2016, 08:58 AM
auditio auditio is offline Search in first column in table Windows 7 32bit Search in first column in table Office 2013
Novice
Search in first column in table
 
Join Date: Jan 2016
Posts: 3
auditio is on a distinguished road
Default

I want to search in all the tables
Multiple search (search input stored in a array).

Want to msgbox like,

With Selection If .Information(wdWithInTable) = False Then MsgBox "The cursor must be within a table cell.", , "Cursor outside table" Exit Sub

Trial code for two input search

Dim oText As String
Dim oSrch 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)
oText = InputBox(Prompt:="Enter the text", _
Title:="* Search Box *", _
Default:="")
oSrch = InputBox(Prompt:="Enter BD No", _
Title:="* Search Box *", _
Default:="")
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, oSrch) > 0 Then
Set oCell = oRow.Cells(4).Range
oCell.End = oCell.End - 1
If Len(oCell) > 0 Then bText = True
oCell.Collapse 0
If bText = True Then
oCell.Text = Chr(32) & oText
Else
oCell.Text = oText
End If
End If
Next i

oSrch = InputBox(Prompt:="Enter 2nd BD No", _
Title:="* Search Box *", _
Default:="")
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, oSrch) > 0 Then
Set oCell = oRow.Cells(4).Range
oCell.End = oCell.End - 1
If Len(oCell) > 0 Then bText = True
oCell.Collapse 0
If bText = True Then
oCell.Text = Chr(32) & oText
Else
oCell.Text = oText
End If
End If
Next i
lbl_Exit:
Set oTable = Nothing
Set oRow = Nothing
Set oCell = Nothing
Set oRng = Nothing
Exit Sub
End Sub
Reply With Quote
Reply

Tags
array, search, table



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to search with column heading then doing the lookup action for below code cmreddy.vba Excel Programming 0 01-20-2015 03:44 AM
VBA to search each row of a word table column Marrick13 Word VBA 7 11-17-2014 04:33 AM
Search in first column in table Macro help - search for value, paste a value in another column IRollman Excel Programming 1 01-14-2014 01:05 PM
Excel Fomula to search for a string and display value from different column zeeshanbutt Excel 1 07-29-2012 12:48 AM
Search in first column in table Need to search a column for a macth and return a result from a third column pdfaust Excel 2 02-03-2011 03:02 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:44 AM.


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