Another Macro Please

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
Is there any way that when I select a range of cells the data will swap places?

i.e if i select the range A1:J5 I want it to change the data over as below on the same sheet


Before

<TABLE style="BACKGROUND-COLOR: #ffffff; PADDING-LEFT: 2pt; PADDING-RIGHT: 2pt; FONT-FAMILY: Calibri,Arial; FONT-SIZE: 11pt" border=1 cellSpacing=0 cellPadding=0><COLGROUP><COL style="WIDTH: 30px"><COL style="WIDTH: 45px"><COL style="WIDTH: 45px"><COL style="WIDTH: 45px"><COL style="WIDTH: 45px"><COL style="WIDTH: 64px"><COL style="WIDTH: 64px"><COL style="WIDTH: 67px"><COL style="WIDTH: 67px"><COL style="WIDTH: 67px"><COL style="WIDTH: 67px"></COLGROUP><TBODY><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt"><TD> </TD><TD>A</TD><TD>B</TD><TD>C</TD><TD>D</TD><TD>E</TD><TD>F</TD><TD>G</TD><TD>H</TD><TD>I</TD><TD>J</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">1</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD> </TD><TD> </TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">2</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD> </TD><TD> </TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">3</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD> </TD><TD> </TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">4</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD> </TD><TD> </TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">5</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD> </TD><TD> </TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD></TR></TBODY></TABLE>

After

<TABLE style="BACKGROUND-COLOR: #ffffff; PADDING-LEFT: 2pt; PADDING-RIGHT: 2pt; FONT-FAMILY: Calibri,Arial; FONT-SIZE: 11pt" border=1 cellSpacing=0 cellPadding=0><COLGROUP><COL style="WIDTH: 30px"><COL style="WIDTH: 67px"><COL style="WIDTH: 67px"><COL style="WIDTH: 67px"><COL style="WIDTH: 67px"><COL style="WIDTH: 64px"><COL style="WIDTH: 64px"><COL style="WIDTH: 45px"><COL style="WIDTH: 45px"><COL style="WIDTH: 45px"><COL style="WIDTH: 45px"></COLGROUP><TBODY><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt"><TD> </TD><TD>A</TD><TD>B</TD><TD>C</TD><TD>D</TD><TD>E</TD><TD>F</TD><TD>G</TD><TD>H</TD><TD>I</TD><TD>J</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">1</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD> </TD><TD> </TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">2</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD> </TD><TD> </TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">3</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD> </TD><TD> </TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">4</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD> </TD><TD> </TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD></TR><TR style="HEIGHT: 18px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">5</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD>GOODBYE</TD><TD> </TD><TD> </TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD><TD>HELLO</TD></TR></TBODY></TABLE>

 
But what I want it to do is, I select a range of cells like above run the macro then the data changes place also like above.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Then you can go to VBE, delete the earlier macro and use this one:

Code:
Sub Dazzawm()
Dim Rng1 As Range, Temp
Dim Rng2 As Range
If Selection.Columns.Count > 1 And Selection.Columns.Count Mod 2 = 0 Then
     Set Rng1 = Selection.Resize(, Selection.Columns.Count / 2)
       Set Rng2 = Rng1.Offset(, Selection.Columns.Count / 2)
            Temp = Rng1.Value
                Rng1 = Rng2.Value
                    Rng2 = Temp
End If
End Sub

This macro is essentially the same thing but won't run automatically whenever your selection changes.
 
Upvote 0
Thanks thats almost correct but I will have 2 blank columns inbetween the data like the table at the top
 
Upvote 0
Try this:

Code:
Sub Dazzawm()
Dim Rng1 As Range, Temp
Dim Rng2 As Range
If Selection.Columns.Count > 2 And Selection.Columns.Count Mod 2 = 0 Then
     Set Rng1 = Selection.Resize(, Selection.Columns.Count / 2 - 1)
       Set Rng2 = Rng1.Offset(, Selection.Columns.Count / 2 + 1)
            Temp = Rng1.Value
                Rng1 = Rng2.Value
                    Rng2 = Temp
End If
End Sub
 
Upvote 0
The Code I have sent is a "SelectionChange_Event" Macro and is held in the WorkSheet Module.
If you right click you sheet and Select "View Code" you will see the "SelectionChange_ Events" Code.
To run the Code You only need to select the range of cells you wish to swap over.
Once you have selected a range with an Even Number of columns, the code will run.
Unfortunately this will happed every time you select an Even number of columns in a range.
If you want the code to run as a module you can select or from a command Button let me know.
Mick
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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