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!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi,

Here is a code that will do what you have asked:

VBA Code:
Sub MoveLastCellToTop()
    Dim lastRow As Long
    Dim columnToCheck As Integer
    
    ' Set the column number to check
    columnToCheck = 1 ' Change this to the column number you want to check
    
    ' Get the last row in the column
    lastRow = Cells(Rows.Count, columnToCheck).End(xlUp).Row
    
    ' Cut the value from the last cell and paste it to the top of the column
    Range(Cells(lastRow, columnToCheck), Cells(lastRow, columnToCheck)).Cut _
        Destination:=Range(Cells(1, columnToCheck), Cells(1, columnToCheck))
End Sub

If this solves your issue, please remember to mark your post as solved. Thanks!

...Mike
 
Upvote 0
Hi,

Here is a code that will do what you have asked:

VBA Code:
Sub MoveLastCellToTop()
    Dim lastRow As Long
    Dim columnToCheck As Integer
   
    ' Set the column number to check
    columnToCheck = 1 ' Change this to the column number you want to check
   
    ' Get the last row in the column
    lastRow = Cells(Rows.Count, columnToCheck).End(xlUp).Row
   
    ' Cut the value from the last cell and paste it to the top of the column
    Range(Cells(lastRow, columnToCheck), Cells(lastRow, columnToCheck)).Cut _
        Destination:=Range(Cells(1, columnToCheck), Cells(1, columnToCheck))
End Sub

If this solves your issue, please remember to mark your post as solved. Thanks!

...Mike
Hey Mike,

The code does move the bottom cell to the top but it deleted the original top cell. How can I have the bottom cell rotate to the top without deleting?
Thank you for the quick response. I appreciate you.
 
Upvote 0
My apologies. It's getting late and I missed that... This should get you there.

VBA Code:
Sub MoveLastCellToTop()
    Dim lastRow As Long
    Dim columnToCheck As Integer
    
    ' Set the column number to check
    columnToCheck = 1 ' Change this to the column number you want to check
    
    ' Get the last row in the column
    lastRow = Cells(Rows.Count, columnToCheck).End(xlUp).Row
    
    ' Insert a new row at the top of the column
    Rows("1:1").Insert Shift:=xlDown
    
    ' Cut the value from the last cell and paste it to the top of the column
    Range(Cells(lastRow, columnToCheck), Cells(lastRow, columnToCheck)).Cut _
        Destination:=Range(Cells(1, columnToCheck), Cells(1, columnToCheck))
End Sub
 
Upvote 0
What about this?

VBA Code:
Sub BottomToTop()
  Const Col As String = "A" '<- Set your column of interest here
  
  Range(Col & Rows.Count).End(xlUp).Cut
  Range(Col & 1).Insert
End Sub
 
Upvote 0
What about this?

VBA Code:
Sub BottomToTop()
  Const Col As String = "A" '<- Set your column of interest here
 
  Range(Col & Rows.Count).End(xlUp).Cut
  Range(Col & 1).Insert
End Sub
This works but how do I add another column to behave the same under the same macro?
 
Upvote 0
Can you be more specific about exactly what the macro should do, and where it should do it?
 
Upvote 0
Can you be more specific about exactly what the macro should do, and where it should do it?
Hi Peter

I want the macro to scan column B (for example) from top to bottom. Identify first non-empty cell as the beginning of a list, find last cell of that list and move it to the beginning of the list. Then continue scanning down until it finds another list and does the same move.
I've attached an image to illustrate what the macro should do. Thanks in advance.
 
Upvote 0
Hi Peter

I want the macro to scan column B (for example) from top to bottom. Identify first non-empty cell as the beginning of a list, find last cell of that list and move it to the beginning of the list. Then continue scanning down until it finds another list and does the same move.
I've attached an image to illustrate what the macro should do. Thanks in advance.
 

Attachments

  • Image 24.02.23 at 22.45.jpeg
    Image 24.02.23 at 22.45.jpeg
    78.6 KB · Views: 1
Upvote 0
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)
 
Upvote 0
Solution

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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