Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #9  
Old 04-11-2019, 08:59 AM
jrooney7 jrooney7 is offline Copying text from last row of multiple tables of different lengths Windows 7 64bit Copying text from last row of multiple tables of different lengths Office 2013
Novice
Copying text from last row of multiple tables of different lengths
 
Join Date: Sep 2018
Posts: 23
jrooney7 is on a distinguished road
Default

That's a great suggestion. I changed one line of your code to

Code:
If InStr(aRng.Text, "Worksheet") > 0 And InStr(aTbl.Range.Cells(aTbl.Range.Cells.Count).Range.Text, "n/a") = 0 Then
This keeps the macro from pulling any n/a results. I've added a header to the others that were missing it, like the Comparison Results one.

In case you're interested, here is the final code I came up with. I used your code to help me format the paragraphs in the destination cell so it's much easier to read. You probably could have done it alot "prettier", but hey, it works

Code:
Sub Results2()
  Dim i As Integer, iTables As Integer
  Dim aTbl As Table, tblLast As Table, tblTgt As Table, aCellTgt As Cell
  Dim aRng As Range, aRngEnd As Range, aRngSource As Range
  
  iTables = ActiveDocument.Tables.Count
  Set tblLast = ActiveDocument.Tables(iTables)
  Set tblTgt = tblLast.Tables(2)
  Set aCellTgt = tblTgt.Cell(1, 2)
  aCellTgt.Range.Text = ""    'if you want to start with a blank cell
  
  For i = 1 To iTables - 1
    Set aTbl = ActiveDocument.Tables(i)
    Set aRng = aTbl.Range.Cells(1).Range
    If InStr(aRng.Text, "Worksheet") > 0 And InStr(aTbl.Range.Cells(aTbl.Range.Cells.Count).Range.Text, "n/a") = 0 Then
      Set aRngSource = aTbl.Range.Cells(aTbl.Range.Cells.Count).Range
      aRngSource.MoveEnd Unit:=wdCharacter, Count:=-1
      Set aRngEnd = aCellTgt.Range
      aRngEnd.Collapse Direction:=wdCollapseEnd
      aRngEnd.MoveEnd Unit:=wdCharacter, Count:=-1
      aRngEnd.FormattedText = aRngSource.FormattedText
      aRngEnd.InsertAfter vbCr & vbCr
    End If
  Next i
  tblLast.Tables(2).Cell(1, 2).Select
  With Selection.Font
    .Bold = False
    .Name = "Times New Roman"
    .Size = 10
    .ColorIndex = wdBlack
    With aCellTgt.Range
        .ParagraphFormat.SpaceAfter = 0
        .ParagraphFormat.SpaceBefore = 0
    End With
  End With
  tblLast.Tables(2).Cell(1, 2).Select
    Selection.HomeKey Unit:=wdLine
    Selection.TypeParagraph
  tblLast.Tables(2).Cell(1, 2).Select
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.Text = "Methodology"
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.Text = "The following methodologies were used in the examination of this case:"
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.Text = "Visual Examination"
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeParagraph
    Selection.Text = "Physical Examination"
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeParagraph
    Selection.Text = "Physical Measurements"
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeParagraph
    Selection.Text = "Microscopic Examination"
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeParagraph
    Selection.Text = "Microscopic Comparison"
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeParagraph
    Selection.Text = "Database Search"
    
  Dim p As Integer, pParagraph As Integer
  Dim r As Range
  tblLast.Tables(2).Cell(1, 2).Select
  pParagraph = Selection.Paragraphs.Count
  tblLast.Tables(2).Cell(1, 2).Range.InsertParagraphAfter
    For p = 1 To pParagraph
      Set aParagraph = tblLast.Tables(2).Cell(1, 2).Range.Paragraphs(p)
      Set aRng = aParagraph.Range
        If InStr(aRng.Text, "Result") > 0 Then
            aRng.Select
            With Selection.Font
                .Bold = True
            End With
        End If
        If InStr(aRng.Text, "Result") = 0 Then
            aRng.Select
            Selection.Paragraphs.LeftIndent = 18
        End If
        If InStr(aRng.Text, "Methodology") > 0 Then
            aRng.Select
            With Selection.Font
                .Bold = True
            End With
            Selection.Paragraphs.LeftIndent = 0
        End If
    Next p
  Selection.Document.Undo
  Selection.Paragraphs.LeftIndent = 18
  Selection.EndKey Unit:=wdLine
  Selection.MoveDown Unit:=wdLine, Count:=1
  Selection.TypeBackspace
  
End Sub

Thank you so much for your help!
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compile text from multiple tables into a cell in a nested table jrooney7 Word VBA 2 03-11-2019 07:55 AM
Copying text from last row of multiple tables of different lengths Copying text into multiple cells at once BIMwit Word Tables 1 05-14-2015 09:50 PM
Ink to Text lines are different lengths raineysky OneNote 0 02-12-2015 11:21 AM
Copying text from last row of multiple tables of different lengths Copying multiple files as text without extensions Metamag Office 3 05-09-2011 06:25 PM
Copying text from last row of multiple tables of different lengths Copying Multiple tables from excel into a single word document dineshtgs Word Tables 1 04-07-2011 01:27 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:50 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft