Reverse paste column VBA

Jacob45678

New Member
Joined
Sep 17, 2022
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hi I would like to reverse paste a column, but the column is going downwards when pasted.

Below is a code referred from other post but its not working :(


Sub RevPast()

Dim Rws As Long
Dim cols As Long
Dim Dest As Range
Dim SRng As Range
Dim i As Long
Dim r As Long

r = 1
Rws = Selection.Rows.Count
cols = Selection.Columns.Count
Set SRng = Selection
Set Dest = Application.InputBox("Select start point of paste range", Type:=8)

For i = Rws To 1 Step -1
SRng.Rows(i).Copy Dest(r)
r = r + 1
Next i

End Sub

1663403071587.png

1663403250495.png
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Looking at your second screenshot, I wouldn't call the operation a reverse copy.
I'd call it: copy to location ending in a selected cell. If this descripion is fine (If I understood your needs well) - try such code:

VBA Code:
Sub PasteFromLastCellUpwards()
Dim Rws As Long
Dim Dest As Range
Dim SRng As Range

Set SRng = Selection
Rws = SRng.Rows.Count
Set Dest = Application.InputBox("Select end point of paste range", Type:=8)
if Dest.row <Rws then
  msgbox "Sorry the copy wouldn't fit above",vbcritical
else
 SRng.Copy Dest.offset(-Rws+1,0)
end if
End Sub
 
Upvote 0
Solution
Looking at your second screenshot, I wouldn't call the operation a reverse copy.
I'd call it: copy to location ending in a selected cell. If this descripion is fine (If I understood your needs well) - try such code:

VBA Code:
Sub PasteFromLastCellUpwards()
Dim Rws As Long
Dim Dest As Range
Dim SRng As Range

Set SRng = Selection
Rws = SRng.Rows.Count
Set Dest = Application.InputBox("Select end point of paste range", Type:=8)
if Dest.row <Rws then
  msgbox "Sorry the copy wouldn't fit above",vbcritical
else
 SRng.Copy Dest.offset(-Rws+1,0)
end if
End Sub
Works like a charm. Appreciate the kind help.

I have added an extra line to select the desired range to move.

Sub PasteFromLastCellUpwards()
Dim Rws As Long
Dim Dest As Range
Dim SRng As Range

ActiveSheet.Range("C3:C8").Select 'desired range to move
Set SRng = Selection
Rws = SRng.Rows.Count
Set Dest = Application.InputBox("Select end point of paste range", Type:=8)
If Dest.Row < Rws Then
MsgBox "Sorry the copy wouldn't fit above", vbCritical
Else
SRng.Copy Dest.Offset(-Rws + 1, 0)
End If
End Sub
 
Upvote 0
If that's constant range, Select ... Selection is a bad idea. You can directly use the operations on this (known) range.
And please note how usung CODE tags - see small icons next to standard formatting - (bold, Italic, ... smileys, ...) above the text when you write a reply or edit your post.

VBA Code:
Sub PasteFromLastCellUpwards()
Dim Rws As Long, Dest As Range, SRng As Range

Set SRng = ActiveSheet.Range("C3:C8")
Rws = SRng.Rows.Count
Set Dest = Application.InputBox("Select end point of paste range", Type:=8)
If Dest.Row < Rws Then
  MsgBox "Sorry the copy wouldn't fit above", vbCritical
Else
  SRng.Copy Dest.Offset(-Rws + 1, 0)
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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