Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Word Macro to delete specific rows from tables


  • Please log in to reply

#1
dmr316

dmr316

    New Member

  • Member
  • Pip
  • 3 posts
Hello everyone, I hope someone is able to help me.

Every day I work with an RTF export from non-Microsoft software, which is a Word-like document set out with multiple tables. I would open this file in Word 2003.

Unfortunately, a lot of the table rows are redundant (see below description) and it would be really convenient if there was a quick way to delete them all, seeing as these RTF exports can go on for about a hundred pages (which is why I decided not to delete them all by hand).

The rows that need to be deleted are of the form: Blank cell, Blank cell, Blank cell with a tab in it, Cell containing three characters: a space, then a zero, then another space. i.e. with the backwards-P symbol on it's [][][tab arrow][sp 0 sp]

On my first try at macros, I was able to delete just one row like that. However, it won't delete any other rows like that. And finally, because I've got multiple tables all with the same problem, I would dearly like to be able to delete those rows in other, separate, tables in that same document.

Thank you in advance for any help.
  • 0

Advertisements


#2
crooz

crooz

    Member

  • Member
  • PipPipPip
  • 412 posts
Cited from Wikipedia...

The Rich Text Format (often abbreviated RTF) is a document file format developed by Microsoft in 1987 for cross-platform document interchange.


Hi dmr316,
Welcome to Geeks to Go! (G2G).
I'm crooz from Holland (not Michigan!) and will try to help you if I can... at least try and give you some ideas to work with that will help in solving your VBA problem.

First of all, if you could send an example of the ‘RTF’ document that you’re working with would help (do not include any personal information in this document PLEASE! – replace any personal info with e.g. ‘lsdl25uh991hi8nf’ were needed but keep the format of the tabel(s) intact. Supply the extension of the filename too - then we know what type of file we're working with.).
Also, can you supply an example of the VBA code you used where you were able to delete the first blank row. After you supply the needed info, maybe we (I) can go further in helping you with your problem.

It's not always easy to visualize what the problem/situation is from G2G members/visitors - even though you're description WAS quite thorough... I still need some examples.

Tip: It could just be a question of sorting the individual tables alphabetically via VBA (if sorting is desirable), then locating the first empty cell/row, thereby selecting and deleting all the rows to the end of the table. Of course you do this PER table (you mentioned multiple tables) - the tables have to be counted where the same procedure is performed for each table.

We’ll (I’ll) wait for your reply.

Stay cool,
crooz

Edited by crooz, 25 July 2009 - 06:48 PM.

  • 0

#3
dmr316

dmr316

    New Member

  • Topic Starter
  • Member
  • Pip
  • 3 posts
Hi Crooz,

I've attached a page from one of these RTF documents to confirm the layout of the tables on the page, each has 4 columns but different numbers of rows.

I've got two pieces of code as it happens, the first one is an internet standard one for finding each table in a Word document, I feel some of these lines may not be needed in the final answer because I'd just like to have the macro process everything and only prompt me when it's done:

Sub FindTables()
Dim iResponse As Integer
Dim tTable As Table
'If any tables exist, loop through each table in collection.
For Each tTable In ActiveDocument.Tables
tTable.Select
iResponse = MsgBox("Table found. Find next?", 68)
If response = vbNo Then Exit For 'User chose to leave search.
Next
MsgBox prompt:="Search Complete.", buttons:=vbInformation
End Sub

And the second one will delete a row in a table where the third cell is a Tab character (Chr 9):

With ActiveDocument
For i = 1 To .Tables.Count
With .Tables(i)
For j = .Rows.Count To 1 Step -1
If Len(.Cell(j,3).Range.Text) = 3 And Asc(Left(.Cell(j,3).Range.Text, 1)) = 9 Then
.Rows(j).Delete
End If
Next j
End With
Next i
End With

Unfortunately, I'm unable to combine these two pieces of code to delete the "blank" rows in the second and subsequent tables in the RTF document as I keep getting a "Run-time error 5941: The requested member of the collection does not exist". I'm assuming that's VBA telling me the macro is unable to find the next table to check for those "blank" rows.

So as you can see, I feel I'm almost there - I just can't seem to cross the finishing line. Any help you can provide would be great. Thanks.

Attached Files


  • 0

#4
crooz

crooz

    Member

  • Member
  • PipPipPip
  • 412 posts
Hi dmr316,
Worked a couple of hours on your problem today and could only do a quick fix (family situation came up)... still on my system it worked perfectly.

I only used the first part of the VBA code you supplied. When you copy and paste the following code (see below), you'll notice that all code above and below the asteriks is your own code - in between is what I added. It worked as I thought it should according to your instructions, but I found that clicking on the 'Yes' button for every table that was found to be redundant, however that's you're decision if you want it to run that way. I myself would run through the whole list of tables found, delete the empty rows at once... and not even look back, if you know what I mean. Even clicking on 'No' button (and I can understand why you 'might' want to do this), the procedure would still execute and delete the empty rows in the subsequent tables (it would not stop) - then again, I didn't want to change your own code that much. But that situation can always be fixed.

See if what I supplied works on your system. If it's what you require, let me know and mark this thread as SOLVED, if you would.

Here's the code:

Sub FindTables()
Dim iResponse As Integer
Dim tTable As Table
If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdNormalView
Else
ActiveWindow.View.Type = wdNormalView
End If
'If any tables exist, loop through each table in collection.
For Each tTable In ActiveDocument.Tables
tTable.Select
'*****************
On Error GoTo cleanup
varRow = 0
Selection.Find.ClearFormatting
With Selection.Find
.Text = "DK"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
End With
Selection.Find.Execute
varDK = Selection.Text
Selection.EndKey unit:=wdRow
Selection.MoveDown unit:=wdLine, Count:=1
Selection.HomeKey unit:=wdLine
ReDoMe:
'count the total number of empty rows that end in a (space and a) zero.
Selection.MoveRight unit:=wdCharacter, Count:=2, Extend:=wdExtend
varCellToCount = Selection.Text
varCellToCount = Left(varCellToCount, 2)
If varCellToCount <> " 0" Then
Selection.MoveDown unit:=wdLine, Count:=1
Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.HomeKey
GoTo ReDoMe
End If
While varCellToCount = " 0"
Selection.HomeKey
Selection.MoveRight unit:=wdCharacter, Count:=2, Extend:=wdExtend
varCellToCount = Selection.Text
varCellToCount = Left(varCellToCount, 2)
varRow = varRow + 1
Selection.MoveDown unit:=wdLine, Count:=1
Wend
varRow = varRow - 1 'remove one row
Selection.MoveUp unit:=wdLine, Count:=4
tTable.Select
Selection.Find.ClearFormatting
With Selection.Find
.Text = "DK"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
End With
Selection.Find.Execute
Selection.MoveDown unit:=wdLine, Count:=1
For x = 1 To varRow
Selection.SelectRow
Selection.Rows.Delete
Next x
Selection.MoveDown unit:=wdLine, Count:=2
'*****************
iResponse = MsgBox("Table found. Find next?", 68)
If response = vbNo Then Exit For 'User chose to leave search.
Next
MsgBox prompt:="Search Complete.", Buttons:=vbInformation
cleanup:
End Sub


In any case, let me know even if it works.

crooz
  • 0

#5
dmr316

dmr316

    New Member

  • Topic Starter
  • Member
  • Pip
  • 3 posts
Hi Crooz,

Thanks for your help and the extra VBA code. I only left the Yes-click thing in to be sure that my code was doing what it should because the full RTF document is very long. But you're right, once the code works as it should, that check will not be needed any more.

I've just tried your improved version of my code on the example file I uploaded yesterday and while the VBA code does work perfectly on that g2gdoc.rtf file, using the exact same code on the full RTF file that can run for up to 100 pages, it doesn't seem to want to do anything but luckily, I guess, it doesn't give any errors at all, even run-time errors.

To give you some history, the problem I've always had with these RTF files that I'm given is that they're never of the same number of pages, or the same number of tables on a page. Which is why I had those two separate pieces of code, where the first one can find every table regardless of the number of pages; and the second one which should be able to check the table for a tab in the third cell of any row. And to make things interesting, some tables wouldn't need any rows deleted because each row would have valid data (i.e. not a tab in the third cell of any row). Again, I apologise for asking for help with a problem that involves such a complicated way of doing things.

As you say, hopefully this situation can be fixed because we're getting ever closer to a solution with each reply. Thanks again for your help with everything.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP