Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-02-2025, 04:09 PM
macropod's Avatar
macropod macropod is offline Table formatting issue: Windows 10 Table formatting issue: Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default


Try:
Code:
Sub CellMerge()
Application.ScreenUpdating = False
Dim Tbl As Table, Cll As Cell
For Each Tbl In ActiveDocument.Tables
  For Each Cll In Tbl.Range.Cells
    With Cll
      If .ColumnIndex < Tbl.Columns.Count Then
        If .Borders(wdBorderBottom).LineStyle = wdLineStyleNone And _
          .Borders(wdBorderTop).LineStyle = wdLineStyleNone Then
          .Merge MergeTo:=Tbl.Cell(Row:=.RowIndex + 1, Column:=.ColumnIndex)
          .Merge MergeTo:=Tbl.Cell(Row:=.RowIndex - 1, Column:=.ColumnIndex)
        End If
      End If
    End With
  Next
Next
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #2  
Old 05-03-2025, 12:11 PM
ranjan ranjan is offline Table formatting issue: Windows 10 Table formatting issue: Office 2019
Advanced Beginner
Table formatting issue:
 
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
Reply

Tags
table borders

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Table formatting issue: Table Formatting Issue tomohawk Excel 2 02-03-2023 08:24 AM
Table formatting issue: Irritating formatting issue in Table of Contents - page number "dots" disappear Eriks Word 9 04-19-2019 06:18 PM
Table formatting issue: Table style - first, last and odd column formatting issue BartS Word Tables 2 08-16-2016 04:10 AM
TOC Formatting Issue Caroline Word 1 03-15-2011 01:59 PM
TOC Formatting Issue Angstromm Word 0 09-22-2010 11:49 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:42 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