Copy the selected range and paste it in reverse

sofas

Active Member
Joined
Sep 11, 2022
Messages
469
Office Version
  1. 2019
Platform
  1. Windows
Hello, I have this code to select several rows and copy them to the last row on column f
The default number of columns is always 2, meaning I select a specific number of rows from columns (F) and (G) and copy them. What I want is when copying, column F is copied into G and vice versa for column G, as in the attached image

55.png


VBA Code:
Sub macro()
Selection.Copy

With Sheet1.Range("F" & Rows.Count).End(xlUp).Offset(1, 0)
      .PasteSpecial xlPasteValues
   End With

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Would something like this work?

VBA Code:
Sub macro()
    Dim ColA, ColB
   
    With Selection
        If .Columns.Count = 2 Then
            ColA = .Columns(1).Value
            ColB = .Columns(2).Value
           
            .Columns(1).Value = ColB
            .Columns(2).Value = ColA
        End If
    End With
End Sub
 
Upvote 0
Hello, is there any suggestion to implement the order?
@rlv01
Thank you, but the problem is that it reverses the original data. Because the idea is to copy the specified range and paste it in reverse, taking into account that I am working on a table
 
Upvote 0
Try this with a copy of your workbook

VBA Code:
Sub macro_v2()
  Dim rws As Long
  
  rws = Selection.Rows.Count
  Cells(Rows.Count, Selection.Column).End(xlUp).Offset(1).Resize(rws, 2).Value = Application.Index(Selection, Evaluate("row(1:" & rws & ")"), Array(2, 1))
End Sub
 
Upvote 0
Try this with a copy of your workbook

VBA Code:
Sub macro_v2()
  Dim rws As Long
  
  rws = Selection.Rows.Count
  Cells(Rows.Count, Selection.Column).End(xlUp).Offset(1).Resize(rws, 2).Value = Application.Index(Selection, Evaluate("row(1:" & rws & ")"), Array(2, 1))
End Sub
Thank you, it works very well. But I noticed that if there is a value in one of the adjacent columns, the data is pasted after the last row. Empty You must copy after the last row of column F, ignoring the presence of a value in other columns, for example, knowing that the data is in a table
 
Upvote 0
But I noticed that if there is a value in one of the adjacent columns, the data is pasted after the last row.
Well, that is exactly what your code in post 1 does too and your only question was how to reverse the columns, so that is what I did. ;)

Try ..
VBA Code:
Sub macro_v3()
  Dim nextrw As Long
  
  With Selection
    nextrw = .EntireColumn.Find(What:="?*", Searchorder:=xlByRows, Searchdirection:=xlPrevious).Row + 1
    .Offset(nextrw - .Row).Value = Application.Index(Selection, Evaluate("row(1:" & .Rows.Count & ")"), Array(2, 1))
  End With
End Sub
 
Upvote 1
Solution
Well, that is exactly what your code in post 1 does too and your only question was how to reverse the columns, so that is what I did. ;)

Try ..
VBA Code:
Sub macro_v3()
  Dim nextrw As Long
  
  With Selection
    nextrw = .EntireColumn.Find(What:="?*", Searchorder:=xlByRows, Searchdirection:=xlPrevious).Row + 1
    .Offset(nextrw - .Row).Value = Application.Index(Selection, Evaluate("row(1:" & .Rows.Count & ")"), Array(2, 1))
  End With
End Sub
Great, thank you. This is what is really needed 👏👏👏
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

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