PDA

View Full Version : [SOLVED:] Deleting Between Two Words in a Table



Rishek
07-05-2017, 12:51 PM
I am trying to clear all the cells BETWEEN two words, but currently, the Macro is also deleting the two words themselves. Here's what I have:



Sub ClearTechandOrch()
Dim lngStart As Long
Dim lngEnd As Long
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Wrap = wdFindStop
.MatchCase = False
.Text = "TECHNICAL ONSTAGE"
If .Execute = False Then
Exit Sub
End If
Selection.Collapse Direction:=wdCollapseEnd
lngStart = Selection.End
.Text = "MUSIC STAFF"
If .Execute = False Then
Exit Sub
End If
lngEnd = Selection.Start
End With
ActiveDocument.Range(lngStart, lngEnd).Select
Selection.Delete Unit:=wdCharacter, Count:=1


End Sub


The Table is in the format:



TECHNICAL ONSTAGE




clear this row
clear
clear
clear




clear this row




clear this row
clear
clear
clear




MUSIC STAFF and some other text




Any help appreciated.

mana
07-08-2017, 12:15 AM
try1;

> lngStart = Selection.End

lngStart = Selection.End + 2


try2;

> lngStart = Selection.End

lngStart = Selection.Cells(1).Range.End + 1


try3;

> Selection.Collapse Direction:=wdCollapseEnd

Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext


> lngEnd = Selection.Start

Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext
lngEnd = Selection.Start

gmaxey
07-08-2017, 05:40 AM
You might find it easier to use the range object with find and replace:


Sub ClearTechandOrch()
Dim oRng As Range
Dim oRngClear As Range
Dim oCell As Cell, lngIndex As Long
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Wrap = wdFindStop
.Text = "TECHNICAL ONSTAGE"
If .Execute Then
oRng.Collapse wdCollapseEnd
Set oRngClear = ActiveDocument.Range
oRngClear.Start = oRng.End
.Text = "MUSIC STAFF"
If .Execute Then
oRngClear.End = oRng.Start - 1
For lngIndex = 2 To oRngClear.Cells.Count
oRngClear.Cells(lngIndex).Range.Text = vbNullString
Next
End If
End If
End With
End Sub

Rishek
07-08-2017, 11:47 AM
Thanks, Mana and Greg. Works very great. Greg, I can't say I quite understand why it works, but I'm happy nonetheless.