Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-28-2024, 06:58 AM
yacov yacov is offline select until = Windows 10 select until = Office 2016
Competent Performer
select until =
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default 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
Attached Images
File Type: jpg SELECT.jpg (40.4 KB, 17 views)
Reply With Quote
  #2  
Old 01-28-2024, 04:01 PM
Guessed's Avatar
Guessed Guessed is offline select until = Windows 10 select until = 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

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
Reply With Quote
  #3  
Old 01-28-2024, 10:52 PM
yacov yacov is offline select until = Windows 10 select until = Office 2016
Competent Performer
select until =
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

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
Attached Images
File Type: jpg SELECT EX1.jpg (11.8 KB, 13 views)
File Type: jpg SELECT EX2.jpg (288.9 KB, 14 views)
Reply With Quote
  #4  
Old 01-29-2024, 02:24 PM
Guessed's Avatar
Guessed Guessed is offline select until = Windows 10 select until = 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

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
Reply With Quote
  #5  
Old 01-29-2024, 11:00 PM
yacov yacov is offline select until = Windows 10 select until = Office 2016
Competent Performer
select until =
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 01-29-2024, 11:51 PM
yacov yacov is offline select until = Windows 10 select until = Office 2016
Competent Performer
select until =
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

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.
Attached Images
File Type: jpg PLUSMINUS1.jpg (113.2 KB, 13 views)
Reply With Quote
  #7  
Old 01-30-2024, 01:46 AM
vivka vivka is offline select until = Windows 7 64bit select until = Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

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
Note: the macro works starting from the doc's 2nd paragraph.
Reply With Quote
  #8  
Old 01-30-2024, 02:21 AM
vivka vivka is offline select until = Windows 7 64bit select until = Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

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
Reply With Quote
  #9  
Old 01-30-2024, 02:45 AM
yacov yacov is offline select until = Windows 10 select until = Office 2016
Competent Performer
select until =
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

thanks a lot. works good and save time
Reply With Quote
  #10  
Old 01-30-2024, 02:53 AM
vivka vivka is offline select until = Windows 7 64bit select until = Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

You are welcome!
Reply With Quote
  #11  
Old 01-30-2024, 09:15 AM
gmaxey gmaxey is offline select until = Windows 10 select until = Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old 01-30-2024, 09:46 AM
vivka vivka is offline select until = Windows 7 64bit select until = Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

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
Reply With Quote
  #13  
Old 01-30-2024, 02:04 PM
gmaxey gmaxey is offline select until = Windows 10 select until = Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #14  
Old 01-30-2024, 04:18 PM
Guessed's Avatar
Guessed Guessed is offline select until = Windows 10 select until = 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 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
I assume it would be logical to also build in the MyCalculator function as well but I can't work out what the user has selected before running that code.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #15  
Old 01-30-2024, 10:30 PM
yacov yacov is offline select until = Windows 10 select until = Office 2016
Competent Performer
select until =
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

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.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
select until = 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 until = Select a row in a different tab cosmicyes Excel Programming 1 07-09-2018 02:47 AM
select until = How to select row aymanharake Excel Programming 3 02-18-2017 04:09 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:31 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