Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 11-11-2018, 12:45 AM
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: 3,977
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

Your code is almost totally based on Selection objects. If you look at Graham's code, the Selection is immediately stored as a range and then all the following code uses ranges.



If you did the same with your code, you would find the improvements you are seeking.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #17  
Old 11-11-2018, 01:00 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Thanks for answering quickly.
I more or less understand what you say, but I know very little about VBA.
All I can do is use the macro recorder and then edit it...and cross fingers in hope it works.
So could you (or anyone) help me transform my macro the way you suggest ?
Thanks.
Reply With Quote
  #18  
Old 11-11-2018, 03:56 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: 3,977
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

The display HAS to change because you are moving a paragraph where the cursor is sitting to another location but you don't need to move the cursor.
Code:
Sub Move_To_Next_Monday()
  Dim oRng As Range, oSel As Range
  Set oSel = Selection.Range.Paragraphs(1).Range
  Set oRng = ActiveDocument.Range
  oRng.Start = oSel.End
  If oRng.Find.Execute("~Monday") Then
    oRng.Start = oRng.Paragraphs(1).Range.End
    oRng.FormattedText = oSel.FormattedText
    oSel.Delete
  End If

lbl_Exit:
  Set oRng = Nothing
  Set oSel = Nothing
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #19  
Old 11-14-2018, 10:57 PM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Thank you so much, it's gonna help me a lot
Reply With Quote
  #20  
Old 01-27-2019, 08:02 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Hi!
This macro simply is fantastic.
It helps me a lot.

After using it for a while, I dream of a further enhancement:
if the text the macro is looking for (ie : "~Monday" ) can't be found AFTER the paragraph where the cursor is located, could the macro look for it BEFORE, and eventually move the paragraph upwards if necessary ?
Reply With Quote
  #21  
Old 03-18-2019, 03:08 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2016 for Mac
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Hi!

No idea ?
Reply With Quote
  #22  
Old 03-18-2019, 05:47 AM
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: 3,977
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

Plenty of ideas but confident in your ability to adapt the code already provided. We don't help out on forums like this just to do your work - we expect that you at least attempt to use the things you learn.

Have a close look at the code provided and see if you can work out what each line is doing. There is an if statement that could be expanded to have an alternative when the first test is false
If something below then
move it down
ElseIf something above then
move it up
end if

If you were to modify the code, you would need to define a second range towards the top of the doc and test for the string up there.

Make an attempt at creating a second range and use an ElseIf to search that one. If you can't work it out, post a sample of where you got to and we can provide some assistance.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #23  
Old 03-18-2019, 06:04 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2016 for Mac
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Hi,

Thanks for answering.

I understand your state of mind, but, to tell the truth, when I look closely at the code, it makes absolutely no sense to me, because I have almost no basic knowledge in VBA.
All I do with VBA is to record macros and make small changes until they do what I want.
And if I'm not able to like this, I ask for help, or give up.

In this case, I understand the general idea you suggest, but then I just don't know what to do.
I'm not reluctant to making efforts, I just don't know basic concepts...
Reply With Quote
  #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: 3,977
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
  #25  
Old 03-19-2019, 03:12 PM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2016 for Mac
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Hi!
Thank you so much.
And also thanks for all your explanations to help me understand how it works.
It's getting clearer, but I think I'd need to take lessons to be able to get onboard the train (not sure it makes sense in english).
You can't imagine how much time I save with this macro.
Thanks.
Reply With Quote
Reply



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 11:33 PM.


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