ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Modify sub to swap text phrases in cells (https://www.excelbanter.com/excel-programming/333837-modify-sub-swap-text-phrases-cells.html)

Max

Modify sub to swap text phrases in cells
 
Hi guys,

How could the sub below be modified for
it to work with for example: text phrases in A1 and A2?
(It currently works only with numbers/integers in A1 and A2)

Private Sub CommandButton1_Click()
Dim x As Integer
x = Range("a1").Value
Range("a1").Value = Range("a2").Value
Range("a2").Value = x
End Sub

Thanks
--
Rgds
Max
xl 97
---
GMT+8, 1° 22' N 103° 45' E
xdemechanik <atyahoo<dotcom
----



Norman Jones

Modify sub to swap text phrases in cells
 
Hi Max,

To operate on text values, change:

Dim x As Integer


to

Dim x As String



To operate on numbers or strings:

Dim x As variant


Incidentally, unless you know that the numeric values to be switche are
integers, you would be better advised to dim as Double or Single.

---
Regards,
Norman


"Max" wrote in message
...
Hi guys,

How could the sub below be modified for
it to work with for example: text phrases in A1 and A2?
(It currently works only with numbers/integers in A1 and A2)

Private Sub CommandButton1_Click()
Dim x As Integer
x = Range("a1").Value
Range("a1").Value = Range("a2").Value
Range("a2").Value = x
End Sub

Thanks
--
Rgds
Max
xl 97
---
GMT+8, 1° 22' N 103° 45' E
xdemechanik <atyahoo<dotcom
----





kassie

Modify sub to swap text phrases in cells
 
use Dim x as Variant

"Max" wrote:

Hi guys,

How could the sub below be modified for
it to work with for example: text phrases in A1 and A2?
(It currently works only with numbers/integers in A1 and A2)

Private Sub CommandButton1_Click()
Dim x As Integer
x = Range("a1").Value
Range("a1").Value = Range("a2").Value
Range("a2").Value = x
End Sub

Thanks
--
Rgds
Max
xl 97
---
GMT+8, 1° 22' N 103° 45' E
xdemechanik <atyahoo<dotcom
----




Bob Phillips[_7_]

Modify sub to swap text phrases in cells
 
Max,

Just declare x as variant.

--
HTH

Bob Phillips

"Max" wrote in message
...
Hi guys,

How could the sub below be modified for
it to work with for example: text phrases in A1 and A2?
(It currently works only with numbers/integers in A1 and A2)

Private Sub CommandButton1_Click()
Dim x As Integer
x = Range("a1").Value
Range("a1").Value = Range("a2").Value
Range("a2").Value = x
End Sub

Thanks
--
Rgds
Max
xl 97
---
GMT+8, 1° 22' N 103° 45' E
xdemechanik <atyahoo<dotcom
----





Max

Modify sub to swap text phrases in cells
 
Thanks for the detailed tips, Norman !
Works great ..
--
Rgds
Max
xl 97
---
GMT+8, 1° 22' N 103° 45' E
xdemechanik <atyahoo<dotcom
----



Max

Modify sub to swap text phrases in cells
 
Thanks, Kassie !
--
Rgds
Max
xl 97
---
GMT+8, 1° 22' N 103° 45' E
xdemechanik <atyahoo<dotcom
----



Max

Modify sub to swap text phrases in cells
 
Thanks, Bob !
What kept you? Busy elsewhere, huh <bg
--
Rgds
Max
xl 97
---
GMT+8, 1° 22' N 103° 45' E
xdemechanik <atyahoo<dotcom
----



Bob Phillips[_7_]

Modify sub to swap text phrases in cells
 
I don't expect to see you in the programming group Max, otherwise I would
have been there <g

Bob

"Max" wrote in message
...
Thanks, Bob !
What kept you? Busy elsewhere, huh <bg
--
Rgds
Max
xl 97
---
GMT+8, 1° 22' N 103° 45' E
xdemechanik <atyahoo<dotcom
----






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

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