A simple task, but yet it seems not so . . . Swapping to cells around.

Tony Balmer

New Member
Joined
Dec 20, 2018
Messages
5
I have a CSV file which is generated from sketchup. I need to know how to take two columns and compare the cells adjacent to each other in those columns and swap the values around if the left cells value is smaller than the right cell.

Seems easy enough to do, but i just cant get it right !:laugh:
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
In computing to swap two values around you need a placeholder (in this case a third cell) to store the original data.

So you've loaded a CSV file into Excel?

Assume you're swapping columns A and B.

in C1
=IF(A1 < B1, B1, A1)

in D1
=IF( A1 < B1, A1, B1)

copy down the columns C and D

Now copy columns C and D in one go
And paste Special, Paste values over the top of columns A and B.
 
Upvote 0
Solution
If you will have to do this more than once, then perhaps a macro would be more helpful.
Code:
[table="width: 500"]
[tr]
	[td]Sub MaxMin()
  Dim ColA As Variant, ColB As Variant
  ColA = Evaluate(Replace("IF(A1:A#<B1:B#,B1:B#,A1:A#)", "#", Cells(Rows.Count, "A").End(xlUp).Row))
  ColB = Evaluate(Replace("IF(A1:A#>B1:B#,B1:B#,A1:A#)", "#", Cells(Rows.Count, "B").End(xlUp).Row))
  Range("A1").Resize(UBound(ColA)) = ColA
  Range("B1").Resize(UBound(ColA)) = ColB
End Sub[/td]
[/tr]
[/table]

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (MaxMin) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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