Reversing column order...

jake_barnes

Board Regular
Joined
Sep 29, 2005
Messages
55
Here's a quick and easy problem:

I'm trying to grab two-column contiguous cell groups and reverse their order (data in column A moves to column B and vice versa).

I've got this code:

Sub Reverse1()
Dim tcells As Long, mCells As Long, ix As Long, ox As Long
Dim iValue As Variant
tcells = Selection.Count
mCells = tcells / 2
For ix = 1 To mCells
iValue = Selection.Item(ix).Value
ox = tcells + 1 - ix
Selection.Item(ix).Value = Selection.Item(ox).Value
Selection.Item(ox).Value = iValue
Next ix
End Sub

But this code also reverses the row order at the same time. I just need for the cells to reverse column locations, and not row locations. Can anyone help?

Thanks in advance!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Something like this?
Code:
Sub ReverseAB()
Dim LastRow As Long
Dim temp
Dim I
    LastRow = Range("A65536").End(xlUp).Row
    For I = 1 To LastRow
        temp = Range("A" & I).Value
        Range("A" & I).Value = Range("b" & I).Value
        Range("b" & I).Value = temp
    Next I
End Sub
 
Upvote 0
Sort of, but I need to be able to manually select a group of cells (say, two columns and thirty contiguous rows) within a large worksheet, and not just reverse the data in two complete columns.

The code I mentioned above has the basic functionality I need, but it reverses the entire sequence of selected cells in order, so the columns and rows are all reversed. I just need the cells within each row to reverse order.
 
Upvote 0
Can you just do a cut and paste? Something like:
Code:
If Selection.Columns.Count <> 2 Then Exit Sub
Selection.Columns.Item(2).Cut
Selection.Columns.Item(1).Insert Shift:=xlToRight
 
Upvote 0
Try this.
Code:
Sub ReverseSelection()
Dim rng As Range
Dim c As Range
Dim temp

    Set rng = Selection.Resize(Selection.Rows.Count, 1)
        
    For Each c In rng.Cells
    
        temp = c.Value
        c.Value = c.Offset(0, 1).Value
        c.Offset(0, 1).Value = temp
        
    Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,954
Members
448,535
Latest member
alrossman

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