exchange contents of 2 cells

  • Thread starter \jeremy via OfficeKB.com\
  • Start date
J

\jeremy via 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
 
D

Doug Robbins

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
 
S

sf

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.
 
D

Doug Robbins

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Excel Data pastes into 2 cells not 1 1
exchange contents of cells 6
exchange content of cells 1
Two separate indexes? 7
How to unlink TOC from a doc 0
Excel Import Comments 3
Cells with varying number of lines 1
Excel 2 into 1 6

Top