+ Reply to Thread
Results 1 to 5 of 5

Swap range values

  1. #1
    Registered User
    Join Date
    07-25-2005
    Posts
    62

    Swap range values

    What's the best way to swap two ranges. I tried this.

    Dim temp As Range

    ...

    temp = Range(rangeName1)
    Range(rangeName1) = Range(rangeName2)
    Range(rangeName2) = temp

    ...

    and of course it is not working

    Thanks,
    Tommy

  2. #2
    Jim Thomlinson
    Guest

    RE: Swap range values

    You need to use set statements and the copy function. Also you need temporary
    memory to hold the range. Something like this

    Sub SwapRanges()
    On Error GoTo ErrorHandler
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rngTemp As Range
    Dim wksTemp As Worksheet

    Set wksTemp = Worksheets.Add
    Set rngTemp = wksTemp.Range("A1")

    Set rng1 = Sheet1.Range("A1:A10")
    Set rng2 = Sheet1.Range("B1:B10")
    rng1.Copy rngTemp
    rng2.Copy rng1
    wksTemp.UsedRange.Copy rng2
    Application.DisplayAlerts = False
    wksTemp.Delete
    ErrorHandler:
    Application.DisplayAlerts = True
    End Sub
    --
    HTH...

    Jim Thomlinson


    "TommySzalapski" wrote:

    >
    > What's the best way to swap two ranges. I tried this.
    >
    > Dim temp As Range
    >
    > ...
    >
    > temp = Range(rangeName1)
    > Range(rangeName1) = Range(rangeName2)
    > Range(rangeName2) = temp
    >
    > ...
    >
    > and of course it is not working
    >
    > Thanks,
    > Tommy
    >
    >
    > --
    > TommySzalapski
    > ------------------------------------------------------------------------
    > TommySzalapski's Profile: http://www.excelforum.com/member.php...o&userid=25561
    > View this thread: http://www.excelforum.com/showthread...hreadid=390386
    >
    >


  3. #3
    Norman Jones
    Guest

    Re: Swap range values

    Hi Tommy,

    Try something like:

    '=============================>>
    Sub TestIt()
    SwapRanges Range("One"), Range("Two")

    End Sub


    Sub SwapRanges(Rng1 As Range, Rng2 As Range)
    Dim Arr As Variant

    Arr = Rng1

    Rng1 = Rng2.Value
    Rng2 = Arr

    End Sub
    '<<=============================
    ---
    Regards,
    Norman



    "TommySzalapski"
    <[email protected]> wrote in
    message news:[email protected]...
    >
    > What's the best way to swap two ranges. I tried this.
    >
    > Dim temp As Range
    >
    > ..
    >
    > temp = Range(rangeName1)
    > Range(rangeName1) = Range(rangeName2)
    > Range(rangeName2) = temp
    >
    > ..
    >
    > and of course it is not working
    >
    > Thanks,
    > Tommy
    >
    >
    > --
    > TommySzalapski
    > ------------------------------------------------------------------------
    > TommySzalapski's Profile:
    > http://www.excelforum.com/member.php...o&userid=25561
    > View this thread: http://www.excelforum.com/showthread...hreadid=390386
    >




  4. #4
    Jim Thomlinson
    Guest

    Re: Swap range values

    Very nice... Use this code (if you do not need to copy the formats). It is a
    pile more efficient than mine. I was assuming formats had to go with the
    range so I never even thought to use a variant...
    --
    HTH...

    Jim Thomlinson


    "Norman Jones" wrote:

    > Hi Tommy,
    >
    > Try something like:
    >
    > '=============================>>
    > Sub TestIt()
    > SwapRanges Range("One"), Range("Two")
    >
    > End Sub
    >
    >
    > Sub SwapRanges(Rng1 As Range, Rng2 As Range)
    > Dim Arr As Variant
    >
    > Arr = Rng1
    >
    > Rng1 = Rng2.Value
    > Rng2 = Arr
    >
    > End Sub
    > '<<=============================
    > ---
    > Regards,
    > Norman
    >
    >
    >
    > "TommySzalapski"
    > <[email protected]> wrote in
    > message news:[email protected]...
    > >
    > > What's the best way to swap two ranges. I tried this.
    > >
    > > Dim temp As Range
    > >
    > > ..
    > >
    > > temp = Range(rangeName1)
    > > Range(rangeName1) = Range(rangeName2)
    > > Range(rangeName2) = temp
    > >
    > > ..
    > >
    > > and of course it is not working
    > >
    > > Thanks,
    > > Tommy
    > >
    > >
    > > --
    > > TommySzalapski
    > > ------------------------------------------------------------------------
    > > TommySzalapski's Profile:
    > > http://www.excelforum.com/member.php...o&userid=25561
    > > View this thread: http://www.excelforum.com/showthread...hreadid=390386
    > >

    >
    >
    >


  5. #5
    Registered User
    Join Date
    07-25-2005
    Posts
    62
    Thanks guys, yeah I knew I could do it using a new worksheet, but that's clumsy. I'll use Norman's to swap values (all I had to do was change the word Range to the word Variant, so simple, thanks) and if I ever need to swap formats and everything, I'll make a new sheet.
    Last edited by TommySzalapski; 07-27-2005 at 09:24 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1