|
|
Thread Tools | Display Modes |
#16
|
||||
|
||||
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 |
#17
|
|||
|
|||
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. |
#18
|
||||
|
||||
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 |
#19
|
|||
|
|||
Thank you so much, it's gonna help me a lot
|
#20
|
|||
|
|||
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 ? |
#21
|
|||
|
|||
Hi!
No idea ? |
#22
|
||||
|
||||
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 |
#23
|
|||
|
|||
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... |
#24
|
||||
|
||||
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 |
#25
|
|||
|
|||
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. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Identify certain text and move all phrases containing it down a line | Chayes | Word VBA | 2 | 11-26-2013 01:16 PM |
How to give line numbering to only selected text? | garlapati | Word | 5 | 04-04-2011 02:05 PM |