exchange contents of 2 cells



\jeremy via OfficeKB.com\
07-10-2005, 12:05 AM
If i have 2 cells (or groups of cells) is there a way to exchange the
contents or do I have to copy/paste the first contents into empty cell, then
copy/paste second contents into first cell, etc, etc....

Thanks.

Jeremy

--
Message posted via http://www.officekb.com

Doug Robbins
07-10-2005, 12:05 AM
If it's two whole columns that you have to interchange, select the second
one, then use Cut and the Select the first one and use Paste Columns.

If its just one cell, if you run the following code when the selection is in
the lefthand cell, it will make the substitution

Dim A As Range, B As Range, i As Long, j As Long, Atext As String
i = Selection.Information(wdEndOfRangeRowNumber)
j = Selection.Information(wdEndOfRangeColumnNumber)
Set A = Selection.Cells(1).Range
A.End = A.End - 1
Atext = A.Text
Set B = Selection.Tables(1).Cell(i, j + 1).Range
B.End = B.End - 1
Selection.Cells(1).Range = B.Text
Selection.Tables(1).Cell(i, j + 1).Range.Text = Atext


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
""jeremy via OfficeKB.com"" <forum@OfficeKB.com> wrote in message
news:50EB377430091@OfficeKB.com...
> If i have 2 cells (or groups of cells) is there a way to exchange the
> contents or do I have to copy/paste the first contents into empty cell,
> then
> copy/paste second contents into first cell, etc, etc....
>
> Thanks.
>
> Jeremy
>
> --
> Message posted via http://www.officekb.com

sf
07-10-2005, 12:05 AM
On Wed, 6 Jul 2005 21:03:18 +0200, Doug Robbins wrote:

> If it's two whole columns that you have to interchange, select the second
> one, then use Cut and the Select the first one and use Paste Columns.
>
> If its just one cell, if you run the following code when the selection is in
> the lefthand cell, it will make the substitution
>
> Dim A As Range, B As Range, i As Long, j As Long, Atext As String
> i = Selection.Information(wdEndOfRangeRowNumber)
> j = Selection.Information(wdEndOfRangeColumnNumber)
> Set A = Selection.Cells(1).Range
> A.End = A.End - 1
> Atext = A.Text
> Set B = Selection.Tables(1).Cell(i, j + 1).Range
> B.End = B.End - 1
> Selection.Cells(1).Range = B.Text
> Selection.Tables(1).Cell(i, j + 1).Range.Text = Atext

Holy mackeral! I'd drag and drop.

Doug Robbins
07-10-2005, 12:05 AM
There's more ways than one to skin a cat.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
"sf" <sf@gmail.com> wrote in message
news:r7uqc1dvqvcq76gds920kb78hrmftd6308@4ax.com...
> On Wed, 6 Jul 2005 21:03:18 +0200, Doug Robbins wrote:
>
>> If it's two whole columns that you have to interchange, select the
>> second
>> one, then use Cut and the Select the first one and use Paste Columns.
>>
>> If its just one cell, if you run the following code when the selection
>> is in
>> the lefthand cell, it will make the substitution
>>
>> Dim A As Range, B As Range, i As Long, j As Long, Atext As String
>> i = Selection.Information(wdEndOfRangeRowNumber)
>> j = Selection.Information(wdEndOfRangeColumnNumber)
>> Set A = Selection.Cells(1).Range
>> A.End = A.End - 1
>> Atext = A.Text
>> Set B = Selection.Tables(1).Cell(i, j + 1).Range
>> B.End = B.End - 1
>> Selection.Cells(1).Range = B.Text
>> Selection.Tables(1).Cell(i, j + 1).Range.Text = Atext
>
> Holy mackeral! I'd drag and drop.


exchange contents of 2 cells