Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-17-2022, 12:35 PM
karkey karkey is offline insertbefore in all cells and columns Windows 10 insertbefore in all cells and columns Office 2013
Novice
insertbefore in all cells and columns
 
Join Date: Jan 2021
Posts: 19
karkey is on a distinguished road
Default insertbefore in all cells and columns

Code:
Sub InsertTableTags()

Dim tbl As Table
Dim rng As Range
Dim row As row

' Loop through all tables in the document
For Each tbl In ActiveDocument.Tables
    Set rng = tbl.Range

    ' Check if all columns are merged
    If tbl.Rows(2).Cells.Count > 1 Then
        ' Insert <TC> in first row
        rng.Cells(1).Range.InsertBefore Text:="<TC>"
        
    End If

    ' Loop through rows in the table
    For Each row In tbl.Rows
        Set rng = row.Range

        ' Check if current row is the second row
        If row.Index = 2 Then
            ' Check if all columns are merged
            If tbl.Rows(2).Cells.Count > 1 Then
                ' Insert <TCH> in second row
                rng.Cells(1).Range.InsertBefore Text:="<TCH>"
            End If
        ' Check if current row is not the first or last row
        ElseIf row.Index > 2 And row.Index < tbl.Rows.Count Then
            ' Insert <TT> in all cells
            rng.Cells(1).Range.InsertBefore Text:="<TT>"
        ' Check if current row is the last row
        ElseIf row.Index = tbl.Rows.Count Then
            ' Check if all columns are merged
            If tbl.Rows(tbl.Rows.Count).Cells.Count > 1 Then
                ' Insert <TTL> in last row
                rng.Cells(1).Range.InsertBefore Text:="<TTL>"
            End If

            ' Check if "Source", "Notes", or "Note" is found in the last row
            If InStr(rng.Text, "Source") > 0 Or InStr(rng.Text, "Notes") > 0 Or InStr(rng.Text, "Note") > 0 Then
                ' Insert <TTS> in all cells of the last row
                rng.Cells(1).Range.InsertBefore Text:="<TTS>"
            End If
        End If
    Next row
Next tbl

End Sub
while run the above code it insertBefore only in 1st column need to insert in all columns
Attached Files
File Type: docx TableTagTesting.docx (12.1 KB, 2 views)
Reply With Quote
  #2  
Old 12-19-2022, 02:58 PM
Guessed's Avatar
Guessed Guessed is offline insertbefore in all cells and columns Windows 10 insertbefore in all cells and columns Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Your code is only touching the first cell in a row. You need a loop there if you want to touch every cell in a row. For example
Code:
Sub InsertTableTags()
  Dim tbl As Table, rng As Range, row As row, aCell As Cell
  
  ' Loop through all tables in the document
  For Each tbl In ActiveDocument.Tables
    Set rng = tbl.Range
    If tbl.Rows(2).Cells.Count > 1 Then    ' Check if all columns are merged
      rng.Cells(1).Range.InsertBefore Text:="<TC>"        ' Insert <TC> in first row
    End If
  
    ' Loop through rows in the table
    For Each row In tbl.Rows
      Set rng = row.Range
      If row.Index = 2 Then   ' Check if current row is the second row
        If tbl.Rows(2).Cells.Count > 1 Then     ' Check if all columns are merged
          rng.Cells(1).Range.InsertBefore Text:="<TCH>" ' Insert <TCH> in second row
        End If
   
      ElseIf row.Index > 2 And row.Index < tbl.Rows.Count Then   ' Check if current row is not the first or last row
        For Each aCell In row.Cells
          aCell.Range.InsertBefore Text:="<TT>"    ' Insert <TT> in all cells
        Next aCell
   
      ElseIf row.Index = tbl.Rows.Count Then   ' Check if current row is the last row
        If tbl.Rows(tbl.Rows.Count).Cells.Count > 1 Then     ' Check if all columns are merged
          rng.Cells(1).Range.InsertBefore Text:="<TTL>"        ' Insert <TTL> in last row
        End If
      
        ' Check if "Source", "Notes", or "Note" is found in the last row
        If InStr(rng.Text, "Source") > 0 Or InStr(rng.Text, "Notes") > 0 Or InStr(rng.Text, "Note") > 0 Then
          rng.Cells(1).Range.InsertBefore Text:="<TTS>"    ' Insert <TTS> in all cells of the last row
        End If
      End If
    Next row
  Next tbl
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 12-19-2022, 09:05 PM
karkey karkey is offline insertbefore in all cells and columns Windows 10 insertbefore in all cells and columns Office 2013
Novice
insertbefore in all cells and columns
 
Join Date: Jan 2021
Posts: 19
karkey is on a distinguished road
Default Thanks, one more help

For span cells and vertically merge cells. How can we check.

for Example file attached
Attached Files
File Type: docx Table_Example.docx (13.3 KB, 1 views)
Reply With Quote
  #4  
Old 12-19-2022, 11:38 PM
Guessed's Avatar
Guessed Guessed is offline insertbefore in all cells and columns Windows 10 insertbefore in all cells and columns Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Any query on .Rows will throw an error if there are vertical merges. Similarly, .Columns will error if there is a horizontal merge. Perhaps this is what you are trying to do
Code:
Sub InsertTableTags()
  Dim tbl As Table, aCell As Cell
  Dim iRow As Integer, iCol As Integer, iRows As Integer
  
  ' Loop through all tables in the document
  For Each tbl In ActiveDocument.Tables
    iRows = tbl.Rows.Count
    For Each aCell In tbl.Range.Cells
      iRow = aCell.RowIndex
      iCol = aCell.ColumnIndex
      Debug.Print iRow, iCol, aCell.Range.Words(1)
      Select Case iRow
        Case 1
          'do nothing
        Case 2
          'If aCell.Next.RowIndex <= iRow Then    'not sure if you want to manage merged
            aCell.Range.InsertBefore Text:="<TC>"
          'End If
        Case iRows    'last row only
          If aCell.Range.Text Like "*Source*" Or aCell.Range.Text Like "*Note*" Then
            aCell.Range.InsertBefore Text:="<TTS>"
          Else
            aCell.Range.InsertBefore Text:="<TTL>"
          End If
        Case Else  'not first, second or last row
          aCell.Range.InsertBefore Text:="<TT>"
      End Select
    Next aCell
  Next tbl
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
insertbefore in all cells and columns sorting cells in excel into specific columns oscarlimerick Excel 6 05-11-2022 07:26 AM
insertbefore in all cells and columns Problem with merged cells and row height in adjacent two- and three-row columns rolandoftheeld Word Tables 3 08-31-2018 03:55 PM
insertbefore in all cells and columns Relating cells in a row in one sheet to cells in columns on another sheet. mbesspiata3 Excel 2 01-06-2017 05:42 AM
Insertbefore an array? help :( n00bie-n00b Word VBA 21 08-21-2014 08:03 PM
How to merge two columns & replace contents of cells conditionally? mag Excel 3 10-24-2012 01:07 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:16 AM.


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