Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-01-2014, 12:57 PM
Bathroth Bathroth is offline Delete All empty Rows - Print - Undo all Rows deleted Windows 7 32bit Delete All empty Rows - Print - Undo all Rows deleted Office 2013
Novice
Delete All empty Rows - Print - Undo all Rows deleted
 
Join Date: Oct 2014
Posts: 1
Bathroth is on a distinguished road
Default Delete All empty Rows - Print - Undo all Rows deleted

Hey Guys,

at the moment i am doing some new stuff here in my office. At the moment i have to do a Serial/form Letter for an document with a little inventory of all our personals equipment. Since all people have diffrent amount of equipment and accesories i needed to do like an dynamic Table with many combined fields. Like "Model_1" - "Brand_1" .. same for second item and so on.

When i print it later, i dont want to have like 15 empty rows. So i decided to do something about it with VBA. I have now a script - but since im kinda new to this stuff im not sure if this code is well done or if i could do better/faster or something like that.

My Code:

Quote:
Sub PrintAllTest()
Application.ScreenUpdating = False
Dim Tbl As Table, cel As Cell, i As Long, n As Long, fEmpty As Boolean, objUndo As UndoRecord
Set objUndo = Application.UndoRecord
objUndo.StartCustomRecord ("DeleteRows")
With ActiveDocument
For Each Tbl In .Tables
n = Tbl.Columns.Count
For i = n To 1 Step -1
fEmpty = True
For Each cel In Tbl.Columns(i).Cells
If Len(cel.Range.Text) > 2 Then
fEmpty = False
Exit For
End If
Next cel


If fEmpty = True Then Tbl.Columns(i).Delete
Next i
Next Tbl
End With
With ActiveDocument
For Each Tbl In .Tables
n = Tbl.Rows.Count
For i = n To 1 Step -1
fEmpty = True
For Each cel In Tbl.Rows(i).Cells
If Len(cel.Range.Text) > 2 Then
fEmpty = False
Exit For
End If
Next cel
If fEmpty = True Then Tbl.Rows(i).Delete
Next i
Next Tbl
End With
Set cel = Nothing: Set Tbl = Nothing
Application.ScreenUpdating = True
objUndo.EndCustomRecord
Dialogs(wdDialogFilePrint).Show
ActiveDocument.Undo

End Sub
So is this code okay for what i wanting to do? Or is there a more easy way to do like i want to? I made this code from diffrent pages and edited.

Again what i need since i guess tis not clear:
1. Delete all empty Rows
2. Print this page
3. Undo all changes (its a form letter so that i dont need one document per user)

Thats it, please advice me if i can do better. I really tried my best so if theres a more easy way - now is the time to help me

Thanks in advance
Reply With Quote
  #2  
Old 10-01-2014, 01:40 PM
gmaxey gmaxey is offline Delete All empty Rows - Print - Undo all Rows deleted Windows 7 32bit Delete All empty Rows - Print - Undo all Rows deleted Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You can test the whole row instead of the individual cells:

Code:
Sub PrintAllTest()
Dim oTbl As Table, lngIndex As Long, lngCols As Long, oUndo As UndoRecord
  Application.ScreenUpdating = False
  Set oUndo = Application.UndoRecord
  oUndo.StartCustomRecord ("DeleteRows")
  With ActiveDocument
    For Each oTbl In .Tables
      lngCols = oTbl.Columns.Count
      For lngIndex = oTbl.Rows.Count To 1 Step -1
        If Len(oTbl.Rows(lngIndex).Range) = (2 * lngCols) + 2 Then
          oTbl.Rows(lngIndex).Delete
        End If
      Next lngIndex
    Next oTbl
  End With
  Application.ScreenUpdating = True
  oUndo.EndCustomRecord
  MsgBox Dialogs(wdDialogFilePrint).Show
  ActiveDocument.Undo
lbl_Exit:
  Set oUndo = Nothing
  Set oTbl = Nothing
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
delete rows, undo, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete All empty Rows - Print - Undo all Rows deleted Macro to delete all empty rows from all tables braddgood Word VBA 15 10-02-2015 01:54 PM
Find and Delete Rows damaniam Word VBA 1 03-11-2014 06:54 AM
Delete All empty Rows - Print - Undo all Rows deleted Grouping table rows to prevent individual rows from breaking across pages dennist77 Word 1 10-29-2013 11:39 PM
Delete All empty Rows - Print - Undo all Rows deleted Delete all rows but the last. elky1967 Word VBA 14 09-21-2012 05:27 AM
Delete All empty Rows - Print - Undo all Rows deleted Macro to delete rows with all empty cells ubns Excel Programming 2 08-14-2012 02:01 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:26 PM.


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