Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #24  
Old 03-18-2019, 05:05 PM
Guessed's Avatar
Guessed Guessed is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 10 How to move a line to another line that starts with a chain selected in the 1st one? 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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to move a line to another line that starts with a chain selected in the 1st one? Document starts to red line spaces and formatting appears on right side Richtriebe Word 2 03-23-2017 11:53 AM
Bold each line of text that starts with a recurring symbol qubie Word 6 08-26-2016 07:10 AM
Lock line so text does not move to next line saundrals Word 2 06-19-2014 03:59 PM
How to move a line to another line that starts with a chain selected in the 1st one? Identify certain text and move all phrases containing it down a line Chayes Word VBA 2 11-26-2013 01:16 PM
How to move a line to another line that starts with a chain selected in the 1st one? How to give line numbering to only selected text? garlapati Word 5 04-04-2011 02:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:31 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft