Swaping cell values

Jak

Well-known Member
Joined
Apr 5, 2002
Messages
833
I have the following macro that swaps the cells value of adjoining cells in a row when highlighted. I need to be able swap cells contents in a column, i.e.

The current macro will change DAY GOOD to GOOD DAY when I highlight the ajoining cells and run the code on a row. I also need to be able to highlight adjoining cells in a column and perform the same cell transfer of data i.e.

DAY
GOOD

would become

GOOD
DAY

I hope this makes scense, any help appreciated.


Code:
Option Explicit

Sub SwapContents()
    Dim rng As Range
    Dim tmp As String

    For Each rng In Selection.Cells
        If Not (Intersect(rng.Offset(, 1), Selection) Is Nothing) Then
            tmp = rng.Formula

            rng.Formula = rng.Offset(, 1).Formula
            rng.Offset(, 1).Formula = tmp
        End If
    Next
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Change the Offset(,1) to Offset(1,0) in all instances.
 
Upvote 0
Many thanks for the quick response Glenn. Works a treat, cheers.
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,217
Members
448,876
Latest member
Solitario

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