Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel: Swapping columns using VBA

Status
Not open for further replies.

mmtraining

IS-IT--Management
Mar 18, 2002
104
DE
In order to consolidate two tables, I need to swap the columns in one of them so that the information is in the right order. How do I do this? As an example: I want to move column c to be before column b.

Thanx for any ideas :)

Carol
Berlin, Germany
 
Carol - I just recorded a macro of swapping columns and got this:
Columns("H:H").Select
Selection.Cut Destination:=Columns("D:D")

HTH Rgds
Geoff

Si hoc legere scis, nimis eruditionis habes
 
I would use

columns("B").insert
columns("D").copy columns("B")
columns("D").delete
Rob
[flowerface]
 
Here is an example where the column with the active cell is swapped with the column to the right:


Sub SwapColumnRight()
SwapRight ActiveCell
End Sub

Sub SwapRight(rngActiveCell As Range)
Dim rngActiveColumn As Range
'gets entire column
Set rngOriginalColumn = rngActiveCell.EntireColumn
'inserts column 2 columns over from original column
rngOriginalColumn.Offset(0, 2).Insert xlToRight
'cuts original and puts it in two columns over
'NOTE: rngOriginalColumn changes with the cut to the new location
rngOriginalColumn.Cut rngOriginalColumn.Offset(0, 2)
'deletes original location (2 columns to the left)
rngOriginalColumn.Offset(0, -2).Delete
End Sub


HTH,
Scott
 
Thanx a lot, all three of you. You have given me three different ways of doing what I want to do. :)

All the best

Carol
Berlin, Germany
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top