PDA

View Full Version : [SOLVED:] Help With a Range Concept



heedaf
07-25-2017, 05:38 PM
I need help with a range concept that I can't seem to understand. I've got the following code where I'm setting the range to another range.


Sub TestMacro()

Dim oRng, oNewRng As Range
Set oRng = ActiveDocument.Paragraphs(1).Range

Set oNewRng = oRng

oNewRng.MoveStartUnitl cset:= " ", Count:=wdForward 'Moves the start of the range to the first space that is found

oRng.Select 'For testing purposes only

oNewRng.Select 'For testing purposes only

End Sub

I thought the range in oRng would maintain the same range but it follows the oNewRng and both are exactly the same (oRng.select and oNewRng.select highlight the same part of the paragraph).

gmayor
07-25-2017, 09:53 PM
If you set the range to another range it remains with the other range. set the ranges to the same fixed location


Set oRng = ActiveDocument.Paragraphs(1).Range
Set oNewRng = ActiveDocument.Paragraphs(1).Range

gmaxey
07-26-2017, 04:19 AM
heedaf,

A couple of points.

1. When you post code, please post code that will compile.
2. When your declaration statements look like this:
Dim oRng, oRngNew as Range
You have declared 1 variant type variable and 1 range type variable. Use:
Dim oRng as Range, oRngNew as Range
3. Like Graham said or use Duplicate



Sub TestMacro()
Dim oRng As Range, oNewRng As Range
Set oRng = ActiveDocument.Paragraphs(1).Range
Set oNewRng = oRng.Duplicate
oNewRng.MoveStartUntil cset:=" ", Count:=wdForward
oRng.Select
oNewRng.Select
End Sub

heedaf
07-26-2017, 09:08 AM
Thank you for the replies. Duplicate was what I was looking for - can't believe I forgot about it.