Swap Cells

  • Thread starter Thread starter William Wolfe
  • Start date Start date
W

William Wolfe

Is there an easy way to select two cells and swap them?

Thanks,

W Wolfe
 
Depends on your definition of easy. Temporarily, click and drag the
first cell to an empty cell. Click and drag the 2nd cell to the 1st
cell. Click and drag the temporary cell to the 2nd cell.

Depending on how far apart the cells are, it may be easier to use
cut+paste rather than click-and-drag.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Multi-disciplinary business expertise
+ Technology skills
= Optimal solution to your business problem
Recipient Microsoft MVP award 2000-2005
 
Good afternoon William

You could use VBA thus:

Sub Swap()
Dim cval(), cadd()
a = 1
ReDim cval(2), cadd(2)
For Each usrcell In Selection
cval(a) = usrcell.Value
cadd(a) = usrcell.Address
a = a + 1
Next usrcell
Range(cadd(1)).Select
ActiveCell = cval(2)
Range(cadd(2)).Select
ActiveCell = cval(1)
End Sub

Just highlight the two cells and run the macro.

HTH

DominicB
 
This is a bit dangerous -

If only one cell is selected, the data is deleted (by ActiveCell =
cval(2)) and a run-time error is generated by Range(cadd(2)).Select.

Also, if more than two cells are selected, a subscript out of range
error will be generated, though in that case no data is destroyed.

I would at least put a line in like

If Selection.Count <> 2 Then Exit Sub
 

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

Back
Top