ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Swap contents of two cells (https://www.excelbanter.com/excel-discussion-misc-queries/51400-swap-contents-two-cells.html)

Sterling

Swap contents of two cells
 
I need some help with the code for a VBA routine that will swap the contents
of two cells. Here's what I want to be able to do...ctrl-highlight 2 cells
then click on a button that will swap the contents of those two files. They
won't be formulas so I don't have to worry about cell references. I would
prefer that the formatting not be copied, only the values. I've seen code
like this:

Private Sub CommandButton1_Click()
cel1 = InputBox("First cell?")
cel2 = InputBox("Second cell?")
c1 = Range(cel1).Value
c2 = Range(cel2).Value
Range(cel1).Value = c2
Range(cel2).Value = c1
End Sub

But I'd rather not use the InputBox to enter the cells.
Thanks.

Dave Peterson

Swap contents of two cells
 
Maybe something like:

Option Explicit
Private Sub CommandButton1_Click()
Dim Cell1 As Range
Dim Cell2 As Range
Dim Temp As Variant
Dim myRng As Range

Set myRng = Selection

If myRng.Cells.Count < 2 Then
MsgBox "exactly 2 cells!"
Exit Sub
End If

If myRng.Areas.Count = 1 Then
Set Cell1 = myRng.Cells(1)
Set Cell2 = myRng.Cells(2)
Else
Set Cell1 = myRng.Areas(1).Cells(1)
Set Cell2 = myRng.Areas(2).Cells(1)
End If

Temp = Cell1.Value
Cell1.Value = Cell2.Value
Cell2.Value = Temp

End Sub




Sterling wrote:

I need some help with the code for a VBA routine that will swap the contents
of two cells. Here's what I want to be able to do...ctrl-highlight 2 cells
then click on a button that will swap the contents of those two files. They
won't be formulas so I don't have to worry about cell references. I would
prefer that the formatting not be copied, only the values. I've seen code
like this:

Private Sub CommandButton1_Click()
cel1 = InputBox("First cell?")
cel2 = InputBox("Second cell?")
c1 = Range(cel1).Value
c2 = Range(cel2).Value
Range(cel1).Value = c2
Range(cel2).Value = c1
End Sub

But I'd rather not use the InputBox to enter the cells.
Thanks.


--

Dave Peterson

Ron Rosenfeld

Swap contents of two cells
 
On Thu, 20 Oct 2005 07:20:02 -0700, "Sterling"
wrote:

I need some help with the code for a VBA routine that will swap the contents
of two cells. Here's what I want to be able to do...ctrl-highlight 2 cells
then click on a button that will swap the contents of those two files. They
won't be formulas so I don't have to worry about cell references. I would
prefer that the formatting not be copied, only the values. I've seen code
like this:

Private Sub CommandButton1_Click()
cel1 = InputBox("First cell?")
cel2 = InputBox("Second cell?")
c1 = Range(cel1).Value
c2 = Range(cel2).Value
Range(cel1).Value = c2
Range(cel2).Value = c1
End Sub

But I'd rather not use the InputBox to enter the cells.
Thanks.



After selecting two cells with <ctrl-click, try this:

====================
Option Explicit

Sub Swap()
Dim c As Range
Dim temp(1) As Variant
Dim i As Long

If Selection.Count < 2 Then
MsgBox ("Can only select two cells, Try again")
Exit Sub
End If

For Each c In Selection
temp(i) = c.Value
i = i + 1
Next c

For Each c In Selection
i = i - 1
c.Value = temp(i)
Next c

End Sub
===================


--ron

Sterling

Swap contents of two cells
 
Thanks to both of you. Much appreciated.

"Ron Rosenfeld" wrote:

On Thu, 20 Oct 2005 07:20:02 -0700, "Sterling"
wrote:

I need some help with the code for a VBA routine that will swap the contents
of two cells. Here's what I want to be able to do...ctrl-highlight 2 cells
then click on a button that will swap the contents of those two files. They
won't be formulas so I don't have to worry about cell references. I would
prefer that the formatting not be copied, only the values. I've seen code
like this:

Private Sub CommandButton1_Click()
cel1 = InputBox("First cell?")
cel2 = InputBox("Second cell?")
c1 = Range(cel1).Value
c2 = Range(cel2).Value
Range(cel1).Value = c2
Range(cel2).Value = c1
End Sub

But I'd rather not use the InputBox to enter the cells.
Thanks.



After selecting two cells with <ctrl-click, try this:

====================
Option Explicit

Sub Swap()
Dim c As Range
Dim temp(1) As Variant
Dim i As Long

If Selection.Count < 2 Then
MsgBox ("Can only select two cells, Try again")
Exit Sub
End If

For Each c In Selection
temp(i) = c.Value
i = i + 1
Next c

For Each c In Selection
i = i - 1
c.Value = temp(i)
Next c

End Sub
===================


--ron



All times are GMT +1. The time now is 07:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com