#1
|
|||
|
|||
select until =
Hello,
I am writing a very long book, which consists of short paragraphs of a few lines each. The equal sign (=) appears once in each paragraph. I would like to make a macro that selects all the text from the beginning of the paragraph up to the = (equal) sign. by shortcut. Enclosed a file with paragraph for example with the sign = (encircled in a circle). Thanks a lot. Yacov |
#2
|
||||
|
||||
You haven't told us what you want to do with such a non-contiguous selection so I'm going to suggest a non-VBA way to achieve this.
Does your content have any tables in it? If not, I would select all and Convert Text to Table, using the "=" as the separator. This will give you a two column table with everything before the = in one column and everything after in the second column. Then select the column you want. You can convert back to text to re-instate the original layout.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank you for your answer, The book is too heavy for the table, it has nearly 10000 pages (17MB).
I have attached an example of a page from the book and also a short example in English to understand the principle. For each paragraph I need another identical paragraph with addition or subtraction (+ or -). Right now I have to copy the paragraph and paste it and then change the + or - sign. That's why I thought that if I knew how to select the paragraph I could add a copy paste command to the macro. like below: Sub PLUSMINUS() ' ' PLUSMINUS Macro ' ' Selection.Copy Selection.TypeParagraph Selection.Paste End Sub |
#4
|
||||
|
||||
You need to explain exactly what you want to do. Your post asks for "I would like to make a macro that selects all the text from the beginning of the paragraph up to the = (equal) sign. by shortcut." This is half a question and making a selection is hardly useful by itself so we would have to presume there is a reason you want to select that text.
When you select that text, what do you want to happen? Is the macro to work only for the current paragraph where the selection is OR in all selected paragraphs OR in the entire document? Your examples of duplicated paragraphs with - changed to + is also changing the number after the =. How does that happen? Is that where the user needs to type a different number?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
The macro should only work on one paragraph. (which the mouse cursor is on).
1 Select the paragraph. 2 to copy 3 Press Enter to drop a new line. 4 Paste 5 At this stage I can change the sign myself and perform the calculation manually. (Unless it is possible for the macro to also change the sign automatically). 6 Just for information - I use the macro below for the calculation: Sub MyCalculator() Dim aChar As Variant, aRng As Range, iTotal As Long Set aRng = Selection.Range For Each aChar In aRng.Characters Select Case AscW(aChar) Case 1488 To 1497: iTotal = iTotal + AscW(aChar) - 1487 Case 1498: iTotal = iTotal + 20 Case 1499: iTotal = iTotal + 20 Case 1500: iTotal = iTotal + 30 Case 1501: iTotal = iTotal + 40 Case 1502: iTotal = iTotal + 40 Case 1503: iTotal = iTotal + 50 Case 1504: iTotal = iTotal + 50 Case 1505: iTotal = iTotal + 60 Case 1506: iTotal = iTotal + 70 Case 1507: iTotal = iTotal + 80 Case 1508: iTotal = iTotal + 80 Case 1509: iTotal = iTotal + 90 Case 1510: iTotal = iTotal + 90 Case 1511: iTotal = iTotal + 100 Case 1512: iTotal = iTotal + 200 Case 1513: iTotal = iTotal + 300 Case 1514: iTotal = iTotal + 400 Case 45: iTotal = iTotal * -1 'a minus sign End Select Next aChar With frmCalculator .Caption = "Total" .lblResult.Caption = iTotal .lblResult.Font.Size = 24 .Show End With End Sub |
#6
|
|||
|
|||
To simplify the macro the = sign can be ignored I can fix manually.
Example of 4 steps - Step 4 I can also do it manually if it's complicated. |
#7
|
|||
|
|||
Hi! What about this simple little code:
Code:
Sub Test_Macro() 'Copy a range from the current para's start till after "=" and 'paste it after the para. Dim oRng As range Set oRng = selection.Paragraphs(1).range oRng.Start = oRng.Start - 1 selection.Find.ClearFormatting selection.Find.Replacement.ClearFormatting With oRng.Find .text = "^13[!^13]@=" .Replacement.text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True If .Execute Then oRng.Start = oRng.Start + 1 oRng.Copy selection.Paragraphs(1).range.Select selection.Collapse wdCollapseEnd selection.InsertAfter vbCr selection.Collapse selection.PasteAndFormat (wdFormatOriginalFormatting) End If End With End Sub |
#8
|
|||
|
|||
Otherwise, the code should be slightly different:
Code:
Sub Test_Macro() 'Copy a range from the current para's start till after "=" and 'paste it after the para. Dim oRng As range Set oRng = selection.Paragraphs(1).range selection.Find.ClearFormatting selection.Find.Replacement.ClearFormatting With oRng.Find .text = "=" .Replacement.text = "" .Forward = True .Wrap = wdFindStop .Format = False If .Execute Then oRng.Start = selection.Paragraphs(1).range.Start oRng.Copy selection.Paragraphs(1).range.Select selection.Collapse wdCollapseEnd selection.InsertAfter vbCr selection.Collapse selection.PasteAndFormat (wdFormatOriginalFormatting) End If End With End Sub |
#9
|
|||
|
|||
thanks a lot. works good and save time
|
#10
|
|||
|
|||
You are welcome!
|
#11
|
|||
|
|||
vivka,
Wouldn't this work just as well and avoid the selection? Code:
Sub Test_Macro() 'Copy a range from the current para's start till after "=" and 'paste it after the para. Dim oRng As Range Set oRng = Selection.Paragraphs(1).Range Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With oRng.Find .Text = "=" .Forward = True .Wrap = wdFindStop .Format = False If .Execute Then oRng.InsertAfter vbCr oRng.Paragraphs(1).Next.Range.FormattedText = oRng.Paragraphs(1).Range.FormattedText End If End With lbl_Exit: Exit Sub End Sub |
#12
|
|||
|
|||
Yes, Greg, your code is quite interesting! I tried to avoid the selection but couldn't. I've only added two short lines to your code (without them it didn't fulfill its task):
Code:
Sub Test_Macro() 'Copy a range from the current para's start till after "=" and 'paste it after the para. Dim oRng As range Set oRng = selection.Paragraphs(1).range oRng.Copy selection.Find.ClearFormatting selection.Find.Replacement.ClearFormatting With oRng.Find .text = "=" .Forward = True .Wrap = wdFindStop .Format = False If .Execute Then oRng.InsertAfter vbCr oRng.Paragraphs(1).Next.range.FormattedText = oRng.Paragraphs(1).range.FormattedText oRng.Paragraphs(1).range.PasteAndFormat (wdFormatOriginalFormatting) End If End With lbl_Exit: Exit Sub End Sub |
#13
|
|||
|
|||
vivka
perhaps I am not completely sure what the task is. Nevertheless, if it works with your changes, good. Best Regards, Greg Maxey "I am not what happened to me, I am what I choose to become." ~ Carl Jung |
#14
|
||||
|
||||
I would do this with string functions to get the relevant positions of the - and the = characters. I don't see how the find makes it faster.
Code:
Sub DupPara() Dim aRngPara As Range, aRng As Range, iEqual As Integer, iPlus As Integer Set aRngPara = Selection.Paragraphs(1).Range iEqual = InStr(aRngPara.Text, "=") iPlus = InStr(aRngPara.Text, "-") If iEqual > 0 Then Set aRng = ActiveDocument.Range(aRngPara.Start, aRngPara.Start + iEqual) aRngPara.InsertAfter vbCr aRngPara.Collapse Direction:=wdCollapseEnd aRngPara.MoveEnd Unit:=wdCharacter, Count:=-1 aRngPara.FormattedText = aRng.FormattedText aRngPara.Characters(iPlus).Text = "+" End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#15
|
|||
|
|||
Thank you all and may God bless you for the help you gave me in writing the book.
Guessed Can you please make another macro so that when I have a paragraph with a plus it will be duplicated with a minus sign. Like you did just the other way around. I tried to modify it alone without success. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Select until last row | Marcia | Excel Programming | 4 | 08-12-2022 03:54 PM |
Select entire text and then de-select certain Headings? | tq94 | Word | 3 | 02-26-2022 03:47 AM |
Select text between 2 | yacov | Word VBA | 2 | 08-15-2021 10:16 PM |
Select a row in a different tab | cosmicyes | Excel Programming | 1 | 07-09-2018 02:47 AM |
How to select row | aymanharake | Excel Programming | 3 | 02-18-2017 04:09 PM |