Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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


--
TommySzalapski
------------------------------------------------------------------------
TommySzalapski's Profile: http://www.excelforum.com/member.php...o&userid=25561
View this thread: http://www.excelforum.com/showthread...hreadid=390386

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default 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"
wrote in
message news:TommySzalapski.1ssrqk_1122415560.206@excelfor um-nospam.com...

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default 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"
wrote in
message news:TommySzalapski.1ssrqk_1122415560.206@excelfor um-nospam.com...

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Swap range values


Thanks guys, yeah I knew I could do it using a new worksheet, but that'
clumsy. I'll use Norman's to swap values (all I had to do was chang
the word Range to the word Variant, so simple, thanks

--
TommySzalapsk
-----------------------------------------------------------------------
TommySzalapski's Profile: http://www.excelforum.com/member.php...fo&userid=2556
View this thread: http://www.excelforum.com/showthread.php?threadid=39038

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SUMPRODUCT or SUMIF if any values in a range equal any values in another range PCLIVE Excel Worksheet Functions 3 July 15th 09 07:43 PM
Swap values in a stacked bar pivot chart vladi16 Charts and Charting in Excel 1 June 10th 09 07:16 AM
swap values between two cells killdare Excel Worksheet Functions 3 November 26th 07 07:20 AM
There should be a swap function for 2 values Johnny Excel Discussion (Misc queries) 1 July 7th 06 05:56 PM
Search/Filter to find values in another range based on two cell values Andy Excel Programming 2 April 29th 04 04:08 PM


All times are GMT +1. The time now is 04:18 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"