![]() |
|
|
|
#1
|
||||
|
||||
|
OK I get the first part. In your document it collects:
Community Health Systems, Inc. (CYHHZ) That part is easy enough. What I don't understand is the second part; and your revised explanation doesn't make any sense, when related to the document you have provided as an example. If you grab the line that contains TOTAL REVENUE and then grab the complete paragraph, then if the document is any indication, you are not actually collecting anything. Are you saying that the document is not representative and there is more data on the same line as TOTAL REVENUE missing from your example. That would make sense. In which case the following will work: Code:
Option Explicit
Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 23 Jul 2017
Dim oDoc As Document
Dim oNewDoc As Document
Dim oRng As Range, oRng2 As Range
Dim vFind As Variant
Dim fso As Object
Dim strPath As String
Const strFind As String = "Add to watchlist|TOTAL REVENUE"
strPath = Environ("USERPROFILE") & "\Desktop\DataExtract.docx" 'The name of the document to save the extract
Set fso = CreateObject("Scripting.FileSystemObject")
vFind = Split(strFind, "|")
Set oDoc = ActiveDocument
If fso.FileExists(strPath) Then
Set oNewDoc = Documents.Open(FileName:=strPath, AddToRecentFiles:=False)
Else
Set oNewDoc = Documents.Add
oNewDoc.SaveAs FileName:=strPath, FileFormat:=12, AddToRecentFiles:=False
End If
Set oRng = oDoc.Range
With oRng.Find
Do While .Execute(FindText:=vFind(0))
oRng.MoveStart wdParagraph, -2
oNewDoc.Range.InsertAfter _
Left(oRng.Paragraphs(1).Range.Text, _
Len(oRng.Paragraphs(1).Range.Text) - 1)
oRng.Collapse 0
Loop
End With
Set oRng = oDoc.Range
With oRng.Find
Do While .Execute(FindText:=vFind(1))
oRng.End = oRng.Paragraphs(1).Range.End - 1
Set oRng2 = oNewDoc.Range
oRng2.End = oRng2.End - 1
oRng2.Collapse 0
oRng2.Text = vbTab & Trim(Replace(LCase(oRng.Text), _
LCase(vFind(1)), "")) & vbCr
oRng.Collapse 0
Loop
End With
oNewDoc.Close wdSaveChanges
lbl_Exit:
Set fso = Nothing
Set oDoc = Nothing
Set oNewDoc = Nothing
Set oRng = Nothing
Set oRng2 = 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 |
|
#2
|
|||
|
|||
|
its crashing at
oNewDoc.SaveAs FileName:=strPath, FileFormat:=12, AddToRecentFiles:=False the other thing is how i suppose to run this macro should i have both windows open 1) with the master data and the 2nd window blank so i t can extract from 1 and paste in the 2nd one. btw i'm using word 2000 so DOCX i believe is for new version of files. It would have been much easier if the DO LOOP code was just fixed in the file that i attached. The macro that you have written is very sophisticated and advance for my knowledge. Thanks a lot though for your help.
|
|
#3
|
|||
|
|||
|
Here is the output that i'm looking for
attached are both 1) macro file 2) the data file. Thanks Mexco Energy Corporation (MXC) Total Revenue 2,525.363 2,421.792 3,390.005 the Rubicon Project, Inc. (RUBI) Total Revenue 278,221 248,484 125,295 Liberty All-Star Growth Fund, Inc. (ASG) Total Revenue 985.629 28,621.59 13,942.108 Partner Communications Company Ltd. (PTNR) Total Revenue 921,000 1,057,000 1,131,000 SITO Mobile, Ltd. (SITO) Total Revenue 29,426.955 12,805.19 15,809.467 Echelon Corporation (ELON) Total Revenue 32,385 38,804 38,730 TerraForm Global, Inc. (GLBL) Total Revenue 214,317 124,116 39,449 Juniper Pharmaceuticals, Inc. (JNP) Total Revenue 54,573 38,287 33,393 Vivint Solar, Inc. (VSLR) Total Revenue 135,167 64,182 25,258 |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Macro Loop Help
|
Twizzle008 | Word VBA | 15 | 09-18-2015 03:20 PM |
Loop - Row increment
|
jrt | Excel Programming | 1 | 04-16-2015 01:46 PM |
| What's wrong with my loop? | Irrma | Word VBA | 2 | 06-17-2014 06:25 AM |
How to a For loop in VBA
|
Jennifer Murphy | Word VBA | 1 | 01-29-2013 03:30 AM |
| How to use for loop in formula in VBA? | tinfanide | Excel Programming | 1 | 12-06-2011 08:33 AM |