#1
|
|||
|
|||
Selection between dots
Hi
Is there a command in VBA to select text that is between two points? I would like to add this to an existing macro. thanks, Yacov |
#2
|
||||
|
||||
You might need to explain why you need this and what the code is needing it for. You can search for content between two characters or you can find a sentence.
For instance to select the first sentence where the cursor is located Selection.Range.Sentences(1).Select
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thanks, I made another macro version
Sub MyCalculatorSentences() Selection.Range.Sentences(1).Select 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 MsgBox iTotal End Sub |
#4
|
|||
|
|||
Hi Andrew,
I have only one last problem, regarding the macro MyCalculator. In some of the paragraphs I used the sign / to indicate or. That is: A/B-C=D and this means that A=B and therefore should be ignored B. Is there any line that can be added to the macro in order to draw such a version? |
#5
|
||||
|
||||
The easiest way would be to set the counter to zero if we encounter the / character. Effectively this is discarding A and starting counting again.
Code:
Sub MyCalculatorSentences() Selection.Range.Sentences(1).Select 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 Case 47: iTotal = 0 'reset counter to zero if / encountered Case Else: Debug.Print AscW(aChar) 'show unhandled characters in Immediate Window End Select Next aChar MsgBox iTotal End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
|||
|
|||
thanks,
works great when a/b-c=d is there a solution if / character come at the end, like: c-a/b=e |
#7
|
||||
|
||||
Well the obvious way would be to not include b in the selection since this code starts with the selection. The inclusion of the 'select the sentence' line is not such a good idea now that this '/' comes in to play in different positions. If the code just worked with what you selected then we wouldn't need to engineer special solutions to deal with increasingly complex cases.
The previous thread's code solution which processes all paragraphs wouldn't have the benefit of your fiddling the selection. That code could use a different approach by working out whether we have hit the '/' before or after a '-". However, since this code is working with Selection then it is far easier to simply not select the piece you want to ignore.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
Thank you,
I solved this in a simple way. I replaced the / with a paragraph mark. It left me with clean equations. |
#9
|
||||
|
||||
Simple solutions are almost always best
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Dots below tabs. | slaycock | Word | 3 | 12-12-2017 06:12 AM |
Dots in the margin | salival0 | Word | 1 | 01-23-2017 10:20 PM |
Selection of all Text for a specific page in word is spanning selection across pages | ramsgarla | Word VBA | 9 | 12-05-2012 03:23 AM |
Dots appear when typing | mcs | Word | 1 | 11-21-2011 10:04 AM |
dots | musicman91 | Word | 1 | 11-02-2008 11:43 AM |