Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Command Button to Swap Cells

I have a button that has the following VB connected with it. The function
will only swap the values of two cells and not the formats. I would like the
formats to be swapped as well. Any ideas?

Private Sub CommandButton1_Click()
Dim vTemp As Variant
With Selection
If .Count < 2 Then
MsgBox "2 cells only."
Else
If .Areas.Count = 2 Then
vTemp = .Areas(1).Cells.Value
.Areas(1).Cells.Value = .Areas(2).Cells.Value
.Areas(2).Cells.Value = vTemp

Else
vTemp = .Cells(1).Value
.Cells(1).Value = .Cells(2).Value
.Cells(2).Value = vTemp
End If
End If
End With
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Command Button to Swap Cells

How about:

Sub swap_um()
Set r3 = Range("Z100")
i = 1
For Each r In Selection
If i = 1 Then Set r1 = r
If i = 2 Then Set r2 = r
If i = 3 Then Exit For
i = i + 1
Next
r1.Copy r3
r2.Copy r1
r3.Copy r2
End Sub

this uses Z100 as a scratchpad.
--
Gary''s Student - gsnu200782


"Prohock" wrote:

I have a button that has the following VB connected with it. The function
will only swap the values of two cells and not the formats. I would like the
formats to be swapped as well. Any ideas?

Private Sub CommandButton1_Click()
Dim vTemp As Variant
With Selection
If .Count < 2 Then
MsgBox "2 cells only."
Else
If .Areas.Count = 2 Then
vTemp = .Areas(1).Cells.Value
.Areas(1).Cells.Value = .Areas(2).Cells.Value
.Areas(2).Cells.Value = vTemp

Else
vTemp = .Cells(1).Value
.Cells(1).Value = .Cells(2).Value
.Cells(2).Value = vTemp
End If
End If
End With
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Command Button to Swap Cells

Thanks Gary, it works perfect!

"Gary''s Student" wrote:

How about:

Sub swap_um()
Set r3 = Range("Z100")
i = 1
For Each r In Selection
If i = 1 Then Set r1 = r
If i = 2 Then Set r2 = r
If i = 3 Then Exit For
i = i + 1
Next
r1.Copy r3
r2.Copy r1
r3.Copy r2
End Sub

this uses Z100 as a scratchpad.
--
Gary''s Student - gsnu200782


"Prohock" wrote:

I have a button that has the following VB connected with it. The function
will only swap the values of two cells and not the formats. I would like the
formats to be swapped as well. Any ideas?

Private Sub CommandButton1_Click()
Dim vTemp As Variant
With Selection
If .Count < 2 Then
MsgBox "2 cells only."
Else
If .Areas.Count = 2 Then
vTemp = .Areas(1).Cells.Value
.Areas(1).Cells.Value = .Areas(2).Cells.Value
.Areas(2).Cells.Value = vTemp

Else
vTemp = .Cells(1).Value
.Cells(1).Value = .Cells(2).Value
.Cells(2).Value = vTemp
End If
End If
End With
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Command Button to Swap Cells

Here's a version that combines both concepts and cleans up after itself.

Sub SwapCells()
Set r3 = Range("T3") ' Or whereever else isn't being used
i = 1
With Selection
If .Count < 2 Then
MsgBox "2 cells only."
Else
For Each r In Selection
If i = 1 Then Set r1 = r
If i = 2 Then Set r2 = r
If i = 3 Then Exit For
i = i + 1
Next
r1.Copy r3
r2.Copy r1
r3.Copy r2
r3.Clear
End If
End With
End Sub


"Prohock" wrote:

Thanks Gary, it works perfect!

"Gary''s Student" wrote:

How about:

Sub swap_um()
Set r3 = Range("Z100")
i = 1
For Each r In Selection
If i = 1 Then Set r1 = r
If i = 2 Then Set r2 = r
If i = 3 Then Exit For
i = i + 1
Next
r1.Copy r3
r2.Copy r1
r3.Copy r2
End Sub

this uses Z100 as a scratchpad.
--
Gary''s Student - gsnu200782


"Prohock" wrote:

I have a button that has the following VB connected with it. The function
will only swap the values of two cells and not the formats. I would like the
formats to be swapped as well. Any ideas?

Private Sub CommandButton1_Click()
Dim vTemp As Variant
With Selection
If .Count < 2 Then
MsgBox "2 cells only."
Else
If .Areas.Count = 2 Then
vTemp = .Areas(1).Cells.Value
.Areas(1).Cells.Value = .Areas(2).Cells.Value
.Areas(2).Cells.Value = vTemp

Else
vTemp = .Cells(1).Value
.Cells(1).Value = .Cells(2).Value
.Cells(2).Value = vTemp
End If
End If
End With
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
print 20 cells with a command button [email protected] Excel Discussion (Misc queries) 5 February 3rd 08 05:57 PM
How can I add values in 2 cells using a command button Dr.H.Subramanian[_2_] Excel Programming 1 July 17th 05 07:21 AM
sum cells using command button ADD Dr.H.Subramanian[_2_] Excel Programming 1 July 14th 05 03:45 PM
Using Command Button to copy cells Pennington Excel Discussion (Misc queries) 1 April 29th 05 02:30 AM
Non Working Cells after command button rlgh60 Excel Programming 1 November 6th 03 11:01 PM


All times are GMT +1. The time now is 12:47 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"