Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Swap 2 ranges

I have a worksheet of start times where it is sometimes necessary to swap a
rage of 4 cells with another...say A4:D4 with A9:D9.
I've found a macro which swaps only 2 cells.... say A25 with A3 but I don't
know if it could be modified.
All I can think of is a macro which would say copy A4:D4 to empty
cells....J4:M4 (after selecting A4) then I Select A9 which would copy A9:D9
to J9:M9. After this the macro would go J64000 endX1 up cut J9:M9.....A1 end
down , offset (1,0) Paste ....same for J4:M4. Looks inelegant and I'd have
to work out how to select a cell in the middle of a macro.
Any ideas if this macro could be modified?

Sub SwapSelections()
Dim rCell1 As Range
Dim rCell2 As Range
Dim strg1 As String, strg2 As String

If Selection.Cells.Count 2 Or Selection.Cells.Count < 2 Then
MsgBox "Your selection should only contain 2 cells", vbCritical
End
End If

If Selection.Areas.Count 1 Then
Set rCell1 = Selection.Areas(1).Cells(1, 1)
Set rCell2 = Selection.Areas(2).Cells(1, 1)

ElseIf Selection.Rows.Count Selection.Columns.Count Then
Set rCell1 = Selection.Range("A1")
Set rCell2 = Selection.Range("A2")
Else
Set rCell1 = Selection.Range("A1")
Set rCell2 = Selection.Range("B1")
End If


strg1 = rCell1
strg2 = rCell2
rCell1 = strg2
rCell2 = strg1

End Sub


Regards Dennis


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Swap 2 ranges

As long as they won't be adjacent ( or as long as you select them as separate
areas they can be adjacent) and the areas are identical in shape and size

Sub ABC()
Dim v As Variant, v1 as Variant
With Selection
v = .Areas(2).Value
v1 = .Areas(1).Value
.Areas(2) = v1
.Areas(1) = v
End With

End Sub
That should give you the idea. This doesn't copy formatting however.

--
Regards,
Tom Ogilvy

"Dennis Saunders" wrote:

I have a worksheet of start times where it is sometimes necessary to swap a
rage of 4 cells with another...say A4:D4 with A9:D9.
I've found a macro which swaps only 2 cells.... say A25 with A3 but I don't
know if it could be modified.
All I can think of is a macro which would say copy A4:D4 to empty
cells....J4:M4 (after selecting A4) then I Select A9 which would copy A9:D9
to J9:M9. After this the macro would go J64000 endX1 up cut J9:M9.....A1 end
down , offset (1,0) Paste ....same for J4:M4. Looks inelegant and I'd have
to work out how to select a cell in the middle of a macro.
Any ideas if this macro could be modified?

Sub SwapSelections()
Dim rCell1 As Range
Dim rCell2 As Range
Dim strg1 As String, strg2 As String

If Selection.Cells.Count 2 Or Selection.Cells.Count < 2 Then
MsgBox "Your selection should only contain 2 cells", vbCritical
End
End If

If Selection.Areas.Count 1 Then
Set rCell1 = Selection.Areas(1).Cells(1, 1)
Set rCell2 = Selection.Areas(2).Cells(1, 1)

ElseIf Selection.Rows.Count Selection.Columns.Count Then
Set rCell1 = Selection.Range("A1")
Set rCell2 = Selection.Range("A2")
Else
Set rCell1 = Selection.Range("A1")
Set rCell2 = Selection.Range("B1")
End If


strg1 = rCell1
strg2 = rCell2
rCell1 = strg2
rCell2 = strg1

End Sub


Regards Dennis



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Swap 2 ranges

Sub SwapSelections()
Dim aryRange1
Dim aryRange2

aryRange1 = Range("A4:D4")
aryRange2 = Range("A9:D9")

Range("A9:D9") = aryRange1
Range("A4:D4") = aryRange2

End Sub


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Dennis Saunders" wrote in message
...
I have a worksheet of start times where it is sometimes necessary to swap a
rage of 4 cells with another...say A4:D4 with A9:D9.
I've found a macro which swaps only 2 cells.... say A25 with A3 but I
don't know if it could be modified.
All I can think of is a macro which would say copy A4:D4 to empty
cells....J4:M4 (after selecting A4) then I Select A9 which would copy
A9:D9 to J9:M9. After this the macro would go J64000 endX1 up cut
J9:M9.....A1 end down , offset (1,0) Paste ....same for J4:M4. Looks
inelegant and I'd have to work out how to select a cell in the middle of a
macro.
Any ideas if this macro could be modified?

Sub SwapSelections()
Dim rCell1 As Range
Dim rCell2 As Range
Dim strg1 As String, strg2 As String

If Selection.Cells.Count 2 Or Selection.Cells.Count < 2 Then
MsgBox "Your selection should only contain 2 cells", vbCritical
End
End If

If Selection.Areas.Count 1 Then
Set rCell1 = Selection.Areas(1).Cells(1, 1)
Set rCell2 = Selection.Areas(2).Cells(1, 1)

ElseIf Selection.Rows.Count Selection.Columns.Count Then
Set rCell1 = Selection.Range("A1")
Set rCell2 = Selection.Range("A2")
Else
Set rCell1 = Selection.Range("A1")
Set rCell2 = Selection.Range("B1")
End If


strg1 = rCell1
strg2 = rCell2
rCell1 = strg2
rCell2 = strg1

End Sub


Regards Dennis



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Swap 2 ranges

Brilliant Tom (thanks Bob).
And so easy...my macro (just written) works but definitely not elegant with
lots of "select" and relative references.
Best regards , Dennis.


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
swap Gary Excel Worksheet Functions 9 August 2nd 06 03:51 AM
I need to swap last name, first name in a cell to first name last Wink Excel Worksheet Functions 3 May 27th 06 02:52 PM
Swap the X and Y axes LAUR Charts and Charting in Excel 1 May 15th 06 07:21 PM
Swap Cells William Wolfe Excel Discussion (Misc queries) 4 June 16th 05 05:18 PM
Last, First Name Swap LaramieHartmann Excel Programming 1 December 9th 03 03:50 PM


All times are GMT +1. The time now is 02:23 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"