PC Review


Reply
Thread Tools Rate Thread

Command Button to Swap Cells

 
 
Prohock
Guest
Posts: n/a
 
      30th Apr 2008
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 With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      30th Apr 2008
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 With Quote
 
Prohock
Guest
Posts: n/a
 
      30th Apr 2008
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 With Quote
 
Qwerx
Guest
Posts: n/a
 
      13th Jun 2008
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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
print 20 cells with a command button ronbinette@rogers.com Microsoft Excel Misc 5 3rd Feb 2008 05:57 PM
How can I add values in 2 cells using a command button =?Utf-8?B?RHIuSC5TdWJyYW1hbmlhbg==?= Microsoft Excel Programming 1 17th Jul 2005 12:44 PM
How can I add values in 2 cells using a command button =?Utf-8?B?RHIuSC5TdWJyYW1hbmlhbg==?= Microsoft Excel Programming 1 17th Jul 2005 07:21 AM
sum cells using command button ADD =?Utf-8?B?RHIuSC5TdWJyYW1hbmlhbg==?= Microsoft Excel Programming 1 14th Jul 2005 03:45 PM
Non Working Cells after command button rlgh60 Microsoft Excel Programming 1 6th Nov 2003 11:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:49 PM.