Cell Transpore

mole999

Well-known Member
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
A Thought from another thread, anyone have code that would do this (more elegantly)
Code:
Sub Transpose()
Application.OnKey "+^{ALTGR}"
Dim VarA As String
Dim VarB As String
    VarA = Range("A1")
    VarB = Range("B1")
    Range("A1") = VarB
    Range("B1") = VarA
End Sub

two cells selected, on a short cut key (something appropriate), swap the data via code

seems simple though I don't recall seeing anything like this on the forum
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
One less variable

Code:
Sub Transpose()
Application.OnKey "+^{ALTGR}"
Dim Swap As String
    Swap = Range("A1")
    Range("A1") = Range("B1")
    Range("B1") = Swap
End Sub
 
Upvote 0
OK, but I was sort of looking for a way that by selecting two cells (at random) and pressing a short cut, the two would swap, I could never guarantee that i would only ever use just two cells
 
Upvote 0
A Thought from another thread, anyone have code that would do this (more elegantly)
Code:
Sub Transpose()
Application.OnKey "+^{ALTGR}"
Dim VarA As String
Dim VarB As String
    VarA = Range("A1")
    VarB = Range("B1")
    Range("A1") = VarB
    Range("B1") = VarA
End Sub

two cells selected, on a short cut key (something appropriate), swap the data via code

seems simple though I don't recall seeing anything like this on the forum

Give this a try...
Code:
Sub SwapTwoCells()
  Dim Temp As Variant
  If Selection.Cells.Count <> 2 Then Exit Sub
  If Selection.Areas.Count = 2 Then
    Temp = Selection.Areas(1)
    Selection.Areas(1) = Selection.Areas(2)
    Selection.Areas(2) = Temp
  Else
    Temp = Selection(1)
    Selection(1) = Selection(2)
    Selection(2) = Temp
  End If
End Sub
 
Upvote 0
Perfect
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Sub SwapTwoCells()
  Dim Temp As Variant
  If Selection.Cells.Count <> 2 Then Exit Sub
  If Selection.Areas.Count = 2 Then
    Temp = Selection.Areas(1)
    Selection.Areas(1) = Selection.Areas(2)
    Selection.Areas(2) = Temp
  Else
    Temp = Selection(1)
    Selection(1) = Selection(2)
    Selection(2) = Temp
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top