Thread: [Solved] Table formatting issue:
View Single Post
 
Old 05-03-2025, 12:11 PM
ranjan ranjan is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: May 2021
Posts: 80
ranjan is on a distinguished road
Default

Hi,

Thanks for the response.

There are some issues like table has not having same inner grid lines which do not print for each cell borders. some cells have inner grid which do not print and some cells might not have.

I have tried the below code to encounter the two errors while executing a macro:

1. Requested member of the collection does not exist error at For j = 1 To tbl.Rows(i).Cells.count

2. cannot access individual rows in this collection because the table has vertically merged cells error at For j = 1 To tbl.Rows(i).Cells.count

Code:
Sub CellMerge()
    Dim doc As Document
    Dim tbl As Table
    Dim nestedTbl As Table
    Dim i As Integer
    Dim j As Integer
    
    ' Set the document to the active document
    Set doc = ActiveDocument
    
    ' Ensure there is at least one table in the document
    If doc.Tables.Count > 0 Then
        ' Reference the first table
        Set tbl = doc.Tables(1)
        
        ' Loop through each row in the main table
        For i = 1 To tbl.Rows.Count
            ' Loop through each cell in the row
            For j = 1 To tbl.Rows(i).Cells.Count
                ' Check if the cell contains a nested table
                If tbl.Rows(i).Cells(j).Tables.Count > 0 Then
                    ' Reference the nested table
                    Set nestedTbl = tbl.Rows(i).Cells(j).Tables(1)
                    
                    ' Perform operations on the nested table
                    ' Example: Print the number of rows in the nested table
                    Debug.Print "Nested Table in Row " & i & ", Cell " & j & " has " & nestedTbl.Rows.Count & " rows."
                    
                    ' Example: Merge cells in the nested table if needed
                    If nestedTbl.Rows.Count > 1 Then
                        nestedTbl.Cell(1, 1).Merge nestedTbl.Cell(1, 2)
                    End If
                End If
            Next j
        Next i
    Else
        MsgBox "No tables found in the document."
    End If
End Sub
Macro has to be applied on the active table only not on all the tables in a document and macro has to be removed or merge the inner grid lines which do not print within the cell borders which are to be printed. after remove or merged this inner grids which do not print within the border apply find = "^p" replace ="" (no space) remove paragraphs for the merged cells.

Please review the code and am attaching new attachment for the reference please review once.

Any other idea or suggestion would be highly appreciated.


Manually merging each cells in a table was taking more time, your help is highly appreicated.

PLEASE REVIEW THE ATTACHMENT WITH THE NAME TEST2

THANKS IN ADVANCE............
Attached Files
File Type: docx TEST 2.docx (24.5 KB, 4 views)

Last edited by ranjan; 05-03-2025 at 08:45 PM.
Reply With Quote