Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-27-2021, 01:49 PM
ranjan ranjan is offline Table Deletions Windows 10 Table Deletions Office 2019
Advanced Beginner
Table Deletions
 
Join Date: May 2021
Posts: 76
ranjan is on a distinguished road
Default Table Deletions

HI,



In a document am having 100 tables; if match string is available in table 20 then delete all the above tables from table 1 to table 19 & another match string is available in table 50 then delete all the below tables from 51 to table 100. then document will have only the relevant tables from table 20 to table 50 & we had deleted the unnecessary tables manually, can we automate this task, please suggest me ...

PFA

Thanks in advance.....
Attached Files
File Type: docx Table deletions.docx (56.4 KB, 7 views)
Reply With Quote
  #2  
Old 08-29-2021, 11:28 AM
ranjan ranjan is offline Table Deletions Windows 10 Table Deletions Office 2019
Advanced Beginner
Table Deletions
 
Join Date: May 2021
Posts: 76
ranjan is on a distinguished road
Default

Hi,

Anyone can help in this....

Your help is highly appreciated...
Reply With Quote
  #3  
Old 08-29-2021, 03:32 PM
Guessed's Avatar
Guessed Guessed is offline Table Deletions Windows 10 Table Deletions Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

We've been doing a lot of your homework recently. It is time for you to step up and make some attempts at learning how to help yourself. If you make some effort to actually learn then people will be happy to help you.

Have a go at recording a macro to find the start string. Then select from there back to the top of the document and delete. Do the same for the end string. Look at that code and work out how you might get it to step back to the start or end of the found table. Use the keywords that appear in the recorded code to do searches and find out more about those commands.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 09-01-2021, 11:38 AM
ranjan ranjan is offline Table Deletions Windows 10 Table Deletions Office 2019
Advanced Beginner
Table Deletions
 
Join Date: May 2021
Posts: 76
ranjan is on a distinguished road
Default

Hi,

Can anyone review the below code... Its working partially only...Its not working for the below tables deletion.

Code:
Sub TABLE_TEST()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "AAA"
    .Replacement.Text = ""
    .Format = False
    .Forward = False
    .Wrap = wdFindStop
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
  End With
  Do While .Find.Execute
    If .Information(wdWithInTable) = True Then
      Set Rng = ActiveDocument.Range(0, .Tables(1).Range.Start)
      Do While Rng.Tables.Count > 0
        Rng.Tables(1).Delete
      Loop
      Exit Do
    End If
    .Collapse wdCollapseStart
  Loop
End With
   
   With ActiveDocument.Range
    With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "BBB:"
    .Replacement.Text = ""
    .Format = False
    .Forward = False
    .Wrap = wdFindStop
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
  End With
  
    Do While .Find.Execute
    If .Information(wdWithInTable) = True Then
      Set Rng = ActiveDocument.Range(0, .Tables(1).Range.Start)
            Do While Rng.Tables.Count > 0
        Rng.Tables(1).Delete
      Loop
      Exit Do
    End If
    .Collapse wdCollapseStart
     Loop
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #5  
Old 09-01-2021, 02:58 PM
Guessed's Avatar
Guessed Guessed is offline Table Deletions Windows 10 Table Deletions Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

That's pretty close.
Now you should test this and compare the code with yours to work out what the subtle differences are doing.
Code:
Sub TABLE_TEST()
  Dim Rng As Range, tblStart As Table, tblEnd As Table
  
  Application.ScreenUpdating = False
  Set Rng = ActiveDocument.Range
  With Rng.Find
    .ClearFormatting
    .Text = "AAA:"
    .Format = False
    .Forward = True      'first table with this text
    .Wrap = wdFindStop
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    Do While .Execute
      If Rng.Information(wdWithInTable) = True Then
        Set tblStart = Rng.Tables(1)
        Set Rng = ActiveDocument.Range(0, tblStart.Range.Start - 1)
        'Rng.Select
        Do While Rng.Tables.Count > 0
          Rng.Tables(1).Delete
        Loop
        GoTo EndTable
      End If
      Rng.Collapse wdCollapseEnd
    Loop
  End With
  
EndTable:
  Set Rng = ActiveDocument.Range(tblStart.Range.End, ActiveDocument.Range.End)
  With Rng.Find
    .Text = "BBB:"
    .Format = False
    .Forward = False    'last table with this text
    .Wrap = wdFindStop
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    Do While .Execute
      If Rng.Information(wdWithInTable) = True Then
        Set tblEnd = Rng.Tables(1)
        Set Rng = ActiveDocument.Range(tblEnd.Range.End, ActiveDocument.Range.End)
        Rng.Select
        Do While Rng.Tables.Count > 0
          Rng.Tables(1).Delete
        Loop
        GoTo NowExitSub
      End If
      Rng.Collapse wdCollapseEnd
     Loop
  End With
    
NowExitSub:
  Application.ScreenUpdating = True
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Table Deletions Table deletions based on a string. nmkhan3010 Word Tables 3 05-03-2021 08:57 PM
Table Deletions Track Changes Problem - lines from comments & deletions don't go to right text. Emmryss Word 6 01-17-2019 05:37 PM
Table Deletions large workbook needs restarted every month with deletions and resort without losing formulas llhail Excel Programming 1 06-02-2015 05:34 PM
Table Deletions Stop Show Up Formatting and Insertions&Deletions theta30 Word 6 03-20-2014 03:57 PM
Macro to format additions deletions when Comparing Files Alphacsulb Word VBA 0 08-19-2013 03:03 PM

Other Forums: Access Forums

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