Microsoft Office Forums Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-11-2019, 06:49 AM
aaghd72 aaghd72 is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 10 Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2019
Novice
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save
 
Join Date: Feb 2019
Posts: 16
aaghd72 is on a distinguished road
Question Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save

The same document that I used your suggestions yesterday to number page 3 when it existed, I now would like to know how to delete rows of a table that are not used. The user will enter data in a "MacroButton Field" box and the data will be entered into a row 1. The next "MacroButton Field" will enter data into row 2 and so forth and so forth. In all there are 20 rows in the table. If the user uses 5 rows or 18 rows, I would like them to be able to run a macro to delete the empty rows or have the macro run when they print or save perhaps so it is completed automatically. One of my issues is that each row has the "MacroButton Field text: "Click here to enter first item." Thanks in advance for your assistance.

Last edited by aaghd72; 02-11-2019 at 10:20 AM. Reason: Added pertinent information.
Reply With Quote
  #2  
Old 02-11-2019, 02:19 PM
macropod's Avatar
macropod macropod is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 7 64bit Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,533
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

You could use a macro like:
Code:
Sub DelRows()
Application.ScreenUpdating = False
Dim r As Long
With ActiveDocument.Tables(1)
  For r = .Rows.Count To 1 Step -1
    With .Rows(r)
      If .Range.Fields.Count > 0 Then .Delete
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
Intercepting events like saving and printing, so your table is automatically updated beforehand requires enabling the relevant event handlers. See:
https://wordmvp.com/FAQs/MacrosVBA/AppClassEvents.htm
https://wordmvp.com/FAQs/MacrosVBA/I...tSavePrint.htm
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 02-11-2019, 02:34 PM
Guessed's Avatar
Guessed Guessed is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 10 Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,240
Guessed has a spectacular aura aboutGuessed has a spectacular aura aboutGuessed has a spectacular aura about
Default

And you can't see any relevant thread titles in this forum? Have a look at posts in the last few days.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 02-14-2019, 08:04 AM
aaghd72 aaghd72 is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 10 Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2019
Novice
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save
 
Join Date: Feb 2019
Posts: 16
aaghd72 is on a distinguished road
Default

This VBA works perfectly when I access the Macro window and run it. I explored options yesterday to get it to run when the document is saved or when you click a button but just could not figure out how to accomplish that. I would rather have the empty rows deleted when the file is saved. If you could help me add that code, I would be greatly thankful. I know as I see more of this code, it will bring back what knowledge I have forgotten. Thanks in advance.
Reply With Quote
  #5  
Old 02-14-2019, 01:38 PM
macropod's Avatar
macropod macropod is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 7 64bit Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,533
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Did you check out the links I provided in post #2?
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #6  
Old 02-15-2019, 06:29 AM
aaghd72 aaghd72 is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 10 Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2019
Novice
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save
 
Join Date: Feb 2019
Posts: 16
aaghd72 is on a distinguished road
Default

Yes, I did. Still couldn't apply the code to an event. I guess I need more assistance. It isn't coming to me. Thanks,
Reply With Quote
  #7  
Old 02-15-2019, 06:44 AM
aaghd72 aaghd72 is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 10 Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2019
Novice
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save
 
Join Date: Feb 2019
Posts: 16
aaghd72 is on a distinguished road
Smile Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save

This is the code I am trying. When I saved the file (.docm) it didn't work. What am I doing wrong? Please! Thanks!

Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, _
SaveAsUI As Boolean, Cancel As Boolean)
Sub DelRows()
Application.ScreenUpdating = False
Dim r As Long
With ActiveDocument.Tables(1)
For r = .Rows.Count To 1 Step -1
With .Rows(r)
If .Range.Fields.Count > 0 Then .Delete
End With
Next
End With
Application.ScreenUpdating = True
End Sub
Attached Files
File Type: dotm Try - Dissemination Meeting Agenda Template.dotm (82.9 KB, 5 views)

Last edited by aaghd72; 02-15-2019 at 10:47 AM. Reason: Added Document
Reply With Quote
  #8  
Old 02-17-2019, 03:09 PM
macropod's Avatar
macropod macropod is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 7 64bit Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,533
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Your template has nothing in 'ThisDocument' code module (or any other) to initiate the class module - which hasn't been named appropriately - and even then, the class module is empty. You need to do all the setup as detailed in the links.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #9  
Old 02-18-2019, 11:22 AM
aaghd72 aaghd72 is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 10 Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2019
Novice
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save
 
Join Date: Feb 2019
Posts: 16
aaghd72 is on a distinguished road
Default

I was able to accomplish my task. Thank you very much for all the assistance.
Reply With Quote
  #10  
Old 02-18-2019, 11:25 AM
aaghd72 aaghd72 is offline Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Windows 10 Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Office 2019
Novice
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save
 
Join Date: Feb 2019
Posts: 16
aaghd72 is on a distinguished road
Default Thanks - Got It!

I was able to have Word delete all empty rows. Thanks for your assistance.
Reply With Quote
Reply

Tags
delete, macro, rows

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Macro that deletes all empty rows in selected table MathiasFC Word VBA 2 02-10-2019 04:06 PM
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Delete Empty Table Rows cltay87 Word VBA 4 02-27-2017 04:23 AM
Macro to delete all empty rows from all tables braddgood Word VBA 15 10-02-2015 01:54 PM
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Delete All empty Rows - Print - Undo all Rows deleted Bathroth Word VBA 1 10-01-2014 01:40 PM
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save Macro to delete rows with all empty cells ubns Excel Programming 2 08-14-2012 02:01 AM


All times are GMT -7. The time now is 08:41 AM.


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