Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2024, 03:40 AM
George24 George24 is offline Refer to second table in document Windows 11 Refer to second table in document Office 2021
Novice
Refer to second table in document
 
Join Date: Jan 2024
Posts: 6
George24 is on a distinguished road
Default Refer to second table in document

Hi,


I have a Word document with 2 tables. The macro I want to run should only apply to the second table. How do I do this? Please explain like I'm 5?

Sub DeleteEmptyRows()
Dim tbl As Table, cel As Cell
Dim i As Long, j As Long, n As Long, fEmpty As Boolean
Application.ScreenUpdating = False
With ActiveDocument
For Each tbl In .Tables
n = tbl.Rows.Count
For i = n To 1 Step -1
fEmpty = True
For j = 3 To tbl.Rows(i).Cells.Count
Set cel = tbl.Rows(i).Cells(j)
If Len(cel.Range.Text) > 2 Then
fEmpty = False
Exit For
End If


Next j
If fEmpty = True Then tbl.Rows(i).Delete
Next i
Next tbl
End With
Set cel = Nothing: Set tbl = Nothing
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #2  
Old 01-11-2024, 05:13 AM
vivka vivka is offline Refer to second table in document Windows 7 64bit Refer to second table in document Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi! Try this:
Code:
Sub DeleteEmptyRows_If()
'In the doc's 2nd tbl, delete rows if any of their cells starting from the 3rd cell is empty.

Dim tbl As Table, cel As cell
Dim i As Long, j As Long, n As Long

    Application.ScreenUpdating = False
    Set tbl = ActiveDocument.range.Tables(2)
    n = tbl.rows.count
    For i = n To 1 Step -1
        For j = 3 To tbl.rows(i).Cells.count
            Set cel = tbl.rows(i).Cells(j)
            If Len(cel.range.text) = 2 Then
                 tbl.rows(i).Delete
                Exit For
             End If
        Next j
    Next i
Set cel = Nothing: Set tbl = Nothing
Application.ScreenUpdating = True
End Sub

Last edited by vivka; 01-11-2024 at 07:37 AM.
Reply With Quote
  #3  
Old 01-11-2024, 05:49 AM
George24 George24 is offline Refer to second table in document Windows 11 Refer to second table in document Office 2021
Novice
Refer to second table in document
 
Join Date: Jan 2024
Posts: 6
George24 is on a distinguished road
Default

Hey,


Thank you for responding!

Unfortunately it didn't work it returns an error on a specific line. I tried implementing your code "Set tbl = ActiveDocument.Range.Tables(2)" in my macro, but this one didn't work either. Do you know if there's another way to refer back to the second table on my document?
Attached Images
File Type: jpg error.JPG (63.5 KB, 8 views)
Reply With Quote
  #4  
Old 01-11-2024, 06:17 AM
vivka vivka is offline Refer to second table in document Windows 7 64bit Refer to second table in document Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Try copy-pasting the updated code again. I've checked it and it runs smoothly. I've imroved the code: now you can select the table and cell to start from:
Code:
Sub Del_Empty_Rows_If_3()
'In the doc's n-th tbl, delete rows if any (at least one) of their cell
'starting from the 3rd cell is empty.

Dim tbl As Table, tblNo As Long, cel As cell, cellNo As Long
Dim i As Long, j As Long, n As Long

    Application.ScreenUpdating = False
    tblNo = InputBox("Enter the table's number:")
    If tblNo > ActiveDocument.range.Tables.count Then
        MsgBox "There's no table with this number in this document."
        Exit Sub
    End If
    Set tbl = ActiveDocument.range.Tables(tblNo)
    cellNo = InputBox("Enter the cell's number to start from:")
    n = tbl.rows.count
    For i = n To 1 Step -1
        For j = cellNo To tbl.rows(i).Cells.count
            Set cel = tbl.rows(i).Cells(j)
            If Len(cel.range.text) = 2 Then
                tbl.rows(i).Delete
                Exit For
            End If
        Next j
    Next i
Set cel = Nothing: Set tbl = Nothing
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #5  
Old 01-11-2024, 07:44 AM
George24 George24 is offline Refer to second table in document Windows 11 Refer to second table in document Office 2021
Novice
Refer to second table in document
 
Join Date: Jan 2024
Posts: 6
George24 is on a distinguished road
Default

The last one did the trick, thank you so much!!!!
Reply With Quote
  #6  
Old 01-11-2024, 08:11 AM
vivka vivka is offline Refer to second table in document Windows 7 64bit Refer to second table in document Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

You are welcome, George24 and kind lurkers!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Refer to second table in document Index hyperlinks refer to old version of document Perkzzz Word 7 11-13-2016 06:54 PM
Refer to second table in document Change Macro so it does not refer to "Active Document" trent.bourne Word VBA 2 10-30-2016 01:49 AM
Refer to second table in document Trying to refer to a placeholder several times throughout a document caitlin Word VBA 2 11-16-2015 04:29 AM
Refer to second table in document Excel table: refer to a particular row? tinfanide Excel 10 08-05-2015 01:17 PM
How do I refer to page numbers, when the numbers change as I prepare the document? StevenD Word 5 11-29-2012 12:52 AM

Other Forums: Access Forums

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