View Single Post
 
Old 03-10-2019, 01:02 PM
jrooney7 jrooney7 is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Sep 2018
Posts: 23
jrooney7 is on a distinguished road
Default

While I was playing with this today, I noticed that which cell it copies in the other tables has do with where the cursor is to begin with. If the cursor is already in the destination cell, it's copying Cell(3, 1) for some reason, 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 - I don't want the end-user having to figure out which is the longest table.

Also, the code is pasting the results contents over previous entries. The sample document has 8 "Worksheet" tables from which the results should be pulled, but only the last "Worksheet" table's results are appearing in the destination cell, so it seems that the results from each "Worksheet" table are being pasted over by the next result - regardless of the length of the result.

I hope this gives some insights. I slightly altered my original code in an attempt to solve my problem, the newer code is below. I am attaching my document if that would help figure out what's going on. The macro is called "Results" and I added a button to the Quick Access Toolbar for the end-user - you can see its location in the attached png.

Code:
Sub Results()
'
' Results Macro
'
'
    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.Paste
                ' 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.Paste
            End If
        End If
    Next
    
End Sub
Attached Images
File Type: png Results compiler macro button.png (27.0 KB, 15 views)
Attached Files
File Type: dotm Firearms Unit Worksheet (for upload).dotm (398.2 KB, 8 views)

Last edited by jrooney7; 03-10-2019 at 01:10 PM. Reason: Uploaded cleaned up version of word dotm and updated code
Reply With Quote