![]() |
|
#1
|
|||
|
|||
|
Hi Guys.
Very new to VBA so please be gentle with me and I apologies in advance for any dumb questions. I have a text string in a table cell (actually a file reference) I'd like to select this text then run a find/replace with the selected string as the replace value. Any help would be massively appreciated. Cheers, Chris Barry |
|
#2
|
||||
|
||||
|
The following macro checks the contents of a document against a series of expressions in the second column of the first table in the document, and outputs a count of those matches in the third column of the table. Document contents before the table are not checked (this makes it more flexible for use in a document where you want to exclude the ‘front matter’ from checking). Only minor modifications would be needed to adapt this for Find/Replace purposes.
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim RngDoc As Range, oTbl As Table, strFnd As String, i As Long, j As Long
With ActiveDocument
Set oTbl = .Tables(1)
For i = 1 To oTbl.Rows.Count
strFnd = Split(oTbl.Cell(i, 2).Range.Text, vbCr)(0)
Set RngDoc = ActiveDocument.Range
RngDoc.Start = oTbl.Range.End
With RngDoc
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Text = strFnd
.MatchWholeWord = True
.MatchWildcards = False
.MatchCase = True
.Execute
End With
j = 0
Do While .Find.Found
j = j + 1
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
If j > 0 Then oTbl.Cell(i, 3).Range.Text = j
Next
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Worked like a charm thank you.
|
|
| Tags |
| file location, replace, table |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trying to find a macro that will copy a cell and paste that value to a specific sheet | bryans88 | Excel Programming | 1 | 12-23-2015 01:40 PM |
Find, select, and replace part of text with bold
|
paik1002 | Word VBA | 4 | 12-07-2015 11:24 PM |
VBA Table – Search All Tables - Find & Replace Text in Table Cell With Specific Background Color
|
jc491 | Word VBA | 8 | 09-30-2015 06:10 AM |
| Microsoft Word macro to find text, select all text between brackets, and delete | helal1990 | Word VBA | 4 | 02-05-2015 03:52 PM |
How to find and select text in a document?
|
mkhuebner | Word VBA | 8 | 02-04-2014 08:04 PM |