ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Swapping columns (https://www.excelbanter.com/excel-programming/357235-swapping-columns.html)

Daminc[_62_]

Swapping columns
 

I made this little macro this morning which may help someone who visits
this forum :)

I don't know if this is an efficient way of doing this but then again
I'm only a beginner so I have an excuse ;)


Code:
--------------------
Public Sub swopcolumn()

Dim Rownum As Integer
Dim Rowaddr As Integer
Dim Colnum As Integer
Dim Colladdr As Integer
Dim startcol As Integer
Dim startrow As Integer
Dim firstcell As Variant
Dim secondcell As Variant
Dim looprange As Integer


Rownum = Selection.Rows.Count
Rowaddr = Selection.Rows(Rownum).Row
Colnum = Selection.Columns.Count
Colladdr = Selection.Columns(Colnum).Column
startcol = (Colladdr - Colnum) + 1
startrow = (Rowaddr - Rownum) + 1

For looprange = 1 To Rownum

firstcell = Cells(startrow, startcol)
secondcell = Cells(startrow, Colladdr)

Cells(startrow, startcol) = secondcell
Cells(startrow, Colladdr) = firstcell

startrow = startrow + 1


Next

End Sub
--------------------


This swops the data between the end columns of a selected range.


--
Daminc
------------------------------------------------------------------------
Daminc's Profile: http://www.excelforum.com/member.php...o&userid=27074
View this thread: http://www.excelforum.com/showthread...hreadid=526713



All times are GMT +1. The time now is 11:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com