Can I swap cell info?

listerheb

Active Member
Joined
Aug 22, 2003
Messages
262
I constantly have to swap content from a group of five cells with another group of five cells. What I have been doing is copying from the first five to a blank five, then taking the second five and copying it to the first, then moving the one I moved at the begining to the second set of five, and finally deleting the left over. Is there a way to select both sets and swap the contents? I must do this over a hundred times in a day.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi listerheb:

Welcome to MrExcel Board!

Is it the same two set of cells that you swap the values from, or are these different sts of cells that you swap at different times?

In any event, the following code will swap the values from cell A1:A5 to C1:C5 and then if you want again from c1:c5 to A1:A5 ...
Code:
Sub ySwap()
    Set temp = [d1:d5]
    [A1:A5].Copy temp
    [c1:c5].Copy [a1]
    temp.Copy [c1]
    temp.Clear
End Sub
I am using Temp (cells D1:D5) as a temporary staging area -- and if the cells D1:D5 in your case can not be spared, set the Temp to be another set of 5 cells.

I hope this helps. If I have misunderstood your question -- my apologies!
 
Upvote 0
It is almost never the same set twice. to further complicate things because of the format of the sheet, the cells I have to swap are not all in the same column or row. It's driving me crazy.
 
Upvote 0
Welcome to the Board!

A few questions:

Any chance you can utilize some named ranges? (and build from there...)

Are your repetitive tasks identical in their sequence? I.E. Copy Range A to Range B & Replace, repeat with a different range, etc.? If you can define a sequence to the process then it can be "mapped".

WHY do you have to do this? (Because if it's driving you nuts, then surely someone else will want to figure out why...)

Regardless, try posting some of your worksheet using Colo's HTML maker, which can be found at the bottom of the page. It will give the folks here much better insight into what you are trying to accomplish (and get you an answer that much quicker!)

Hope that helps,

Smitty

P.S. with Yogi on the team, you're sure to get an answer...just spell it out.
 
Upvote 0
Hi listerheb:

So, what we are dealing with are two sets of ranges that have to be swapped, and the ranges could be different every time. if this is the case then, you may want to try ...
Code:
Sub YSwap2()
    swap1 = InputBox("key-in range for Swap1")
    Set swap1 = Range(swap1)
    swap2 = InputBox("key-in range for Swap2")
    Set swap2 = Range(swap2)
    Set temp = [d1:d5]
    swap1.Copy temp
    swap2.Copy swap1
    temp.Copy swap2
    temp.Clear
End Sub
In the following simulation ...
y030822h1.xls
ABCDEFG
1a_1
2a_2Swap1
3a_3Swap2
4a_4Temp
5a_5
6
7b_1
8b_2
9b_3
10b_4
11b_5
Sheet8


The macro prompts me to key-in a range for Swap1, and Swap2, then set those ranges accordingly and then proceeds to swap the values between Swap1 and Swap2.

My macro requires selecting the ranges on prompts to Input Boxes, and use a Temp range -- based on your project constraints, and/or personal preferences, this method may or may not work for you. However, you may be able to adapt it to suit your needs/constraints. If I have misunderstood your question -- my apologies!
 
Upvote 0
That is so close. It's working for regular cells however some of the cells in my sheet are merged. I get a can't copy merged cell error. I will put up pics of the sheet tomorrow from home. I can't do it from work. Unless you know already how to get around that.
 
Upvote 0
Hi listerheb:

Ah -- merged cells!

You are going to run into some problems with merged cells. In any event, the following macro will swap the values from two ranges, each 1 column wide and 5 rows deep -- one or both ranges may consist of merged cells ...
Code:
Sub YSwap3()
    
    '**** Yogi Anand on Aug-23-2003 *********************************************
    '*    swaps the values from 2 ranges 1 column wide and 5 rows deep          *
    '*    one or both ranges may consist of merged cells
    '****************************************************************************
    Beep
    swap1r = InputBox("key-in range for Swap1")
    [E1:E5] = "=Index(swap1, {1,2,3,4,5}, 1)": Set swapme1 = [E1:E5]: [E1:E5].Value = [E1:E5].Value
    Beep
    swap2r = InputBox("key-in range for Swap2")
    Cells(Range(swap1r).Row, Range(swap1r).Column) = Cells(Range(swap2r).Row, Range(swap2r).Column)
    Cells(Range(swap1r).Row + 1, Range(swap1r).Column) = Cells(Range(swap2r).Row + 1, Range(swap2r).Column)
    Cells(Range(swap1r).Row + 2, Range(swap1r).Column) = Cells(Range(swap2r).Row + 2, Range(swap2r).Column)
    Cells(Range(swap1r).Row + 3, Range(swap1r).Column) = Cells(Range(swap2r).Row + 3, Range(swap2r).Column)
    Cells(Range(swap1r).Row + 4, Range(swap1r).Column) = Cells(Range(swap2r).Row + 4, Range(swap2r).Column)
    Cells(Range(swap2r).Row, Range(swap2r).Column) = WorksheetFunction.Index([E1:E5], 1, 1)
    Cells(Range(swap2r).Row + 1, Range(swap2r).Column) = WorksheetFunction.Index([E1:E5], 2, 1)
    Cells(Range(swap2r).Row + 2, Range(swap2r).Column) = WorksheetFunction.Index([E1:E5], 3, 1)
    Cells(Range(swap2r).Row + 3, Range(swap2r).Column) = WorksheetFunction.Index([E1:E5], 4, 1)
    Cells(Range(swap2r).Row + 4, Range(swap2r).Column) = WorksheetFunction.Index([E1:E5], 5, 1)
    swapme1.Clear
    MsgBox "Done!"
End Sub
I hope this helps. If I have misunderstood your question -- my apologies!
 
Upvote 0
Here is part of the sheet I use. The type of change I need to make is Z7:Z9 with AH12:AH14 it could be any two sets of 5 cells. but the whole sheet looks like that. Is there a way that it will allow me to click and drag to select the cells? That would be the only way to make it faster than doing it manually.
blank football field nov 2003.xls
ZAAABACADAEAFAGAHAIAJAKALAMANAO
7BJ30BJ28LIR27TCP29
81:00PM-5:00PM-7:00PM--
9JoeMarkKelly
10
11
12CS26FPG25MB23CS24
138:00PM-8:00PM-8:30PM--
14SeanJenniferMike
15
16
17RLT14PBJ12FPG11RLT13
18----
19
20
21
Dealer
 
Upvote 0
Are you working on a time sheet?

If so, and please, no offense intended, but it looks like an awfully convoluted way to put it together. (But I'm only looking at columns Z-AN, so I'm probably totally off base with what A-Y represent).

Maybe reconfiguring your worksheet might aid the process.

Sorry for misunderstanding... :oops: It's Saturday, HOT, and I dive into the AC every once in a while after working on the roof! (READ AS: "Brain's still fried!)

Smitty
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,847
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top