Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-09-2019, 07:53 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 Copying text from last row of multiple tables of different lengths

Hello all,

I have a word document (*.dotm) that will have a random number of tables, some of which will have a first row containing the word "Worksheet" - e.g. "Firearm Worksheet", "Bullet Worksheet", etc. The code below is intended to loop through the tables in the document and if it finds one with the word "Worksheet" in the first row, it copies the text in the last row and pastes it to a destination cell in the last table of the document. However, the code as written copies based where the cursor is to begin with. If the cursor is in a table that is not the longest (there is some other table that contains more rows), the code is pulling text based on the length of that table, so it's not the last row. But if I put the cursor in the longest table in the document, it seems to be copying the last row correctly. I'm thinking it's a problem with my NumberOfRowsInCurrentTable setup. Ideally, the code should work regardless of which table the cursor is in, or even if the cursor is not in a table - I don't want the end-user having to figure out which is the longest table. Here is my code:

Code:
Sub Results()
'
' Results Macro
'
'
    Application.ScreenUpdating = False

    Dim LastTable As Integer
       LastTable = ActiveDocument.Range.Tables.Count
    Dim currentTableIndex As Integer
       currentTableIndex = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count
    Dim NumberOfRowsInCurrentTable As Integer
       NumberOfRowsInCurrentTable = ActiveDocument.Range.Tables(currentTableIndex).Rows.Count
    
    Dim t As Table
    
    For Each t In ActiveDocument.Tables
        t.Cell(1, 1).Range.Select
        Selection.Find.Execute FindText:="Worksheet"
        If Selection.Find.Found = True Then
            t.Cell(NumberOfRowsInCurrentTable, 1).Range.Select
            Selection.Range.Copy
            ' this section is for when the destination cell in the Results Table is empty
            If Len(ActiveDocument.Tables(LastTable).Tables(2).Cell(1, 2).Range.Text) = 3 Then
                ActiveDocument.Tables(LastTable).Tables(2).Cell(1, 2).Range.Select
                Selection.EndKey Unit:=wdLine
                Selection.PasteSpecial DataType:=wdPasteText
                ' this section is for when the destination cell in the Results Table already contains text
                ElseIf Len(ActiveDocument.Tables(LastTable).Tables(2).Cell(1, 2).Range.Text) > 3 Then
                ActiveDocument.Tables(LastTable).Tables(2).Cell(1, 2).Range.Select
                Selection.EndKey Unit:=wdLine
                Selection.TypeParagraph
                Selection.TypeParagraph
                Selection.PasteSpecial DataType:=wdPasteText
            End If
        End If
    Next
    
    Application.ScreenUpdating = True

End Sub
Any help is appreciated!
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 06:30 AM.


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