Delete rows between bookmarks

J

JeffG

I have a table that has a number of bookmarks in the first column. Depending
on a condition, I want to delete all the rows between 2 of the bookmarks but
not the row that the bookmark is on. It seems simple but I can’t find the
magic words to make it happen.

Any help would be greatly appreciated.
 
P

Pesach Shelnitz

Hi Jeff,

See if the following macro has the magic that you are seeking.

Sub DeleteRowsBetweenBookmarks()
Dim BkmName1 As String
Dim BkmName2 As String
Dim MyRange As Range
Dim Pos1 As Long
Dim Pos2 As Long

With ActiveDocument
Set MyRange = .Range
BkmName1 = InputBox("Type the name of the first bookmark.")
If .Bookmarks.Exists(BkmName1) = False Then
MsgBox "The first bookmark specified does not exist."
GoTo ErrorHandler
Else
If .Bookmarks(BkmName1).Range.Tables.Count > 0 Then
Pos1 = .Bookmarks(BkmName1).Range.Rows(1).Range.End
Else
MsgBox BkmName1 & " is not in a table."
GoTo ErrorHandler
End If
End If
BkmName2 = InputBox("Type the name of the second bookmark.")
If .Bookmarks.Exists(BkmName2) = False Then
MsgBox "The second bookmark specified does not exist."
GoTo ErrorHandler
Else
If .Bookmarks(BkmName2).Range.Tables.Count > 0 Then
Pos2 = .Bookmarks(BkmName2).Range.Rows(1).Range.start
Else
MsgBox BkmName2 & " is not in a table."
GoTo ErrorHandler
End If
End If
End With
If Pos2 < Pos1 Then
MsgBox "There are no rows to delete"
GoTo ErrorHandler
End If
MyRange.start = Pos1
MyRange.End = Pos2
MyRange.Rows.Delete
ErrorHandler:
Set MyRange = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top