LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 367
Default swap values between two cells

Thanks for the additional info.

yeah you're right, should have taken a sub,
don't know why I didn't.

nice approach without a tempvar.

cheers

Carlo

On Nov 26, 4:08 pm, "Rick Rothstein \(MVP - VB\)"
wrote:
Since you are not going to return a value from your function, it probably
should be a Sub instead.

While the Temp variable method is probably the fastest, I thought the
readers of this thread might find a solution that does not require a
temporary variable of some interest.

If the two cells contain numerical values...

Sub Swap(C1 As Range, C2 As Range)
C1.Value = C1.Value + C2.Value
C2.Value = C1.Value - C2.Value
C1.Value = C1.Value - C2.Value
End Sub

If the two cells contain numerical and/or text values...

Sub Swap(C1 As Range, C2 As Range)
C1.Value = C1.Value & C2.Value
C2.Value = Replace(C1.Value, C2.Value, "")
C1.Value = Replace(C1.Value, C2.Value, "")
End Sub

Rick

"carlo" wrote in message

...



You could have written a little bit more.


if you want to do it in vba use this function


function swap(cell1 as range, cell2 as range)


dim Temp_val as variant
Temp_val = cell1.value
cell1.value = cell2.value
cell2.value = Temp_val


end function


hth


Carlo


On Nov 26, 12:01 pm, killdare
wrote:- Hide quoted text -


- Show quoted text -


 
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
Is there an easy way to swap the contents of two cells in Excel? N.B. Yond Excel Discussion (Misc queries) 7 September 23rd 06 02:33 PM
There should be a swap function for 2 values Johnny Excel Discussion (Misc queries) 1 July 7th 06 05:56 PM
Swap contents of two cells Sterling Excel Discussion (Misc queries) 3 October 20th 05 06:04 PM
Swap Cells William Wolfe Excel Discussion (Misc queries) 4 June 16th 05 05:18 PM
Is there a quick shortcut to swap the data in two cells? eburris Excel Discussion (Misc queries) 1 January 13th 05 11:38 PM


All times are GMT +1. The time now is 07:49 AM.

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

About Us

"It's about Microsoft Excel"