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 =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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