VBA Column Inversion

Solved
texastina Posted messages 55 Status Member -  
texastina Posted messages 55 Status Member -
Hello,

I would like to know if it is possible to reverse the contents of several columns using VBA?
For example: Column D ==> Column B
Column B ==> Column C
Column C ==> Column D

I hope I was clear enough :)

Thank you for your responses :)

4 answers

Maurice
 
Hello
I prefer to move the column
example:
 Sub MoveCol() Columns(4).Cut Columns(2).Insert Shift:=xlToRight Range("A1").Select End Sub 

See you +
Maurice
2
texastina Posted messages 55 Status Member
 
Thank you for your response; I reproduced them and it works :)
I choose your method; I find it simpler

Thank you and have a nice day :)
0
JvDo Posted messages 1924 Registration date   Status Member Last intervention   859
 
Hello,

Yes, it is possible. You can even limit yourself to the first move: column D in front of column B.
The other columns will automatically be in the right place.

If beyond your question you want to know how to do it, use the macro recorder and you will have the code.

Best regards
1
diablo13800 Posted messages 3469 Registration date   Status Member Last intervention   1 872
 
Hello,

Yes, this is possible thanks to the copy and paste command sequence.

In your case:
dim example as Range // Declaration of your action field example = Range("A1:E600") // Selection of a large range, to be adapted according to the length of your columns. example.Column(4).select // Selects column D Selection.copy // Copies the selected column example.Column(5).select // Selects column E ActiveSheet.Paste // Pastes what has been copied into the selected column


I hope I have been clear enough, do it as you wish :)
Keep me posted if there's anything you don't understand.
0
texastina Posted messages 55 Status Member
 
Thank you for your response :)
You were clear, I understood and reproduced it on my table!

Then I also replicated Maurice's method and I find it simpler :)
0
michel_m Posted messages 18903 Registration date   Status Contributor Last intervention   3 320
 
Hello
try:

Option Explicit
'------------------
Sub bcd_cdb()
Dim Derlig As Integer, T_in, T_out, cptr As Integer

Application.ScreenUpdating = False
Derlig = Cells.Find(what:="*", searchdirection:=xlPrevious).Row
T_in = Range("B1:D" & Derlig)
ReDim T_out(1 To Derlig, 1 To 3)
For cptr = 1 To UBound(T_in)
T_out(cptr, 1) = T_in(cptr, 3) '
T_out(cptr, 2) = T_in(cptr, 1) '
T_out(cptr, 3) = T_in(cptr, 2) '
Next
Range("B1").Resize(UBound(T_in), 3) = T_out

End Sub


--
Michel
0
texastina Posted messages 55 Status Member
 
Hello Mchel
Thank you for your response, it works too :)
0