Help creating macro to cut the last cell in a range with data to the top of the range

Sinaloense

New Member
Joined
Feb 23, 2023
Messages
10
Office Version
  1. 365
Platform
  1. MacOS
Hi all,

I'm looking to create a macro look through a column and cut the last cell containing data and paste to the beginning of the data (top). I know I can do this manually dragging and dropping while holding Shift. I'm fairly new to excel and I would appreciate the help.

Thank you!
 
Thanks for the clarification. I have still made 2 assumptions:
  1. There is no heading like in your image and it is just the lists that appear in the column as per my first mini sheet below, and
  2. The values in the column are not the result of formulas.
VBA Code:
Sub BottomToTop_v2()
  Dim rA As Range
 
  Const Col As String = "B" '<- Set your column of interest here
 
  For Each rA In Columns(Col).SpecialCells(xlConstants).Areas
    rA.Cells(rA.Count).Cut
    rA.Cells(1).Insert
  Next rA
End Sub

Before

Sinaloense.xlsm
B
1
2
31
42
53
64
7
8
9
10A
11B
12C
13D
14E
15F
16
17cat
18dog
19
Sheet1 (2)


After run 1

Sinaloense.xlsm
B
1
2
34
41
52
63
7
8
9
10F
11A
12B
13C
14D
15E
16
17dog
18cat
19
Sheet1 (2)


After run 2

Sinaloense.xlsm
B
1
2
33
44
51
62
7
8
9
10E
11F
12A
13B
14C
15D
16
17cat
18dog
19
Sheet1 (2)
This worked!!
Can I modify the code for it to work on multiple columns and behave the same?
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Can I modify the code for it to work on multiple columns and behave the same?
Sure. I have assumed that each column of interest will have at least one block of data to act on.

VBA Code:
Sub BottomToTop_v3()
  Dim rA As Range
  Dim Col As Variant
  
  Const myCols As String = "B E F J" '<- Set your columns of interest here
  
  Application.ScreenUpdating = False
  For Each Col In Split(myCols)
    For Each rA In Columns(Col).SpecialCells(xlConstants).Areas
      rA.Cells(rA.Count).Cut
      rA.Cells(1).Insert
    Next rA
  Next Col
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Sure. I have assumed that each column of interest will have at least one block of data to act on.

VBA Code:
Sub BottomToTop_v3()
  Dim rA As Range
  Dim Col As Variant
 
  Const myCols As String = "B E F J" '<- Set your columns of interest here
 
  Application.ScreenUpdating = False
  For Each Col In Split(myCols)
    For Each rA In Columns(Col).SpecialCells(xlConstants).Areas
      rA.Cells(rA.Count).Cut
      rA.Cells(1).Insert
    Next rA
  Next Col
  Application.ScreenUpdating = True
End Sub
Thanks man
You’re the best!!
 
Upvote 0
Cheers. Glad to help. Thanks for the follow-up. :)
 
Upvote 0
One last thing, how would I reverse the movement. (Top to bottom)?
Try this

VBA Code:
Sub TopToBottom_v1()
  Dim rA As Range
  Dim Col As Variant
  
  Const myCols As String = "B E F J" '<- Set your columns of interest here
  
  Application.ScreenUpdating = False
  For Each Col In Split(myCols)
    For Each rA In Columns(Col).SpecialCells(xlConstants).Areas
      rA.Cells(1).Cut
      rA.Cells(rA.Count + 1).Insert
    Next rA
  Next Col
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this

VBA Code:
Sub TopToBottom_v1()
  Dim rA As Range
  Dim Col As Variant
 
  Const myCols As String = "B E F J" '<- Set your columns of interest here
 
  Application.ScreenUpdating = False
  For Each Col In Split(myCols)
    For Each rA In Columns(Col).SpecialCells(xlConstants).Areas
      rA.Cells(1).Cut
      rA.Cells(rA.Count + 1).Insert
    Next rA
  Next Col
  Application.ScreenUpdating = True
End Sub
Once again, Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,214,411
Messages
6,119,356
Members
448,888
Latest member
Arle8907

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