View Single Post
 
Old 03-18-2019, 05:05 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I've added comments to describe a bit about what is going on
Code:
Sub Move_To_Next_Monday()
  Dim oRng As Range, oSel As Range, oRngUp As Range
  Set oSel = Selection.Range.Paragraphs(1).Range  'creates a range of 1st paragraph of selection
  Set oRng = ActiveDocument.Range 'creates a range of entire doc
  oRng.Start = oSel.End   'shortens this range to the just the content after the selection
  Set oRngUp = ActiveDocument.Range   'creates a different range of entire doc
  oRngUp.End = oSel.Start   'shortens this new range to just the content above the selection
  
  If oRng.Find.Execute("~Monday") Then      'see if the string appears below the selection
    'note that oRng collapses to the found text range
    oRng.Start = oRng.Paragraphs(1).Range.End  'now move the range to the position where we want to copy the para
    oRng.FormattedText = oSel.FormattedText   'copy the para into the new location
    oSel.Delete                               'remove the para from the original position
    
  ElseIf oRngUp.Find.Execute("~Monday") Then  'otherwise see if the string appears above the selection
    If oRngUp.Paragraphs(1).Next.Range <> oSel Then 'make sure the selection is not immediately below found range
      oRngUp.Start = oRngUp.Paragraphs(1).Range.End
      oRngUp.FormattedText = oSel.FormattedText
      oSel.Delete
    End If
  End If

lbl_Exit:
  Set oRng = Nothing
  Set oRng = Nothing
  Set oSel = Nothing
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote