VBA Column Inversion

Solved
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

  1. Contributor
    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
    1. Hello Mchel
      Thank you for your response, it works too :)
      0
  2. 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
    1. 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
  3. 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
    1. 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
  4. 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