Move Row to Bottom in Excel If Cell Contains a Value

If you are searching for the solution or some special tricks to move row to bottom if a cell contains a specific value in Excel then you have landed in the right place. There are some quick steps to move row to bottom if the cell contains a specific value in Excel. This article will show you every step with proper illustrations so you can easily apply them for your purpose. Let’s get into the central part of the article.


How to Move Row to Bottom If Cell Contains a Value: 2 Examples

Suppose, you have a dataset that contains the customer name, product brand name and sales amount, and the target status. Some cells of the target column are filled with text “ Passed”  in the rows which meet the target amount. And, you want to move rows to the bottom in which Target column cells are filled with “Passed” text.

Excel Move Row to Bottom If Cell Contains a Value

In this section, I will show you the quick and easy steps to move from row to bottom if the cell contains a specific value in Excel on the Windows operating system. You will find detailed explanations with clear illustrations of each thing in this article. I have used the Microsoft 365 version here. But you can use any other versions as of your availability. If anything in this article doesn’t work in your version then leave us a comment.


1. Move Row to Bottom If Cell Contains a Specific Text

You have to use a VBA code to move row to bottom in which cells contain a specific text. Follow the below steps to do this.

Steps:

  • First, go to the top ribbon and press on the Developer then press on the Visual Basic option from the menu.

You can use ALT + F11 to open the Microsoft Visual Basic for Applications window if you don’t have the Developer tab added.

Microsoft Visual Basic for Applications

  • Now, a window named “Microsoft Visual Basic for Applications” will appear. Here from the top menu bar, press on the “Insert” And a menu will appear. From them, select the “Module’” option.

Insert Module

  • Now, a new “Module” window will appear. And Paste this VBA code into the box.
Sub Move_Row_To_End()
Dim xR As Range
Dim xT As String
Dim xC As Range
Dim xER As Long
Dim p As Long
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xT = ActiveWindow.RangeSelection.AddressLocal
Else
xT = ActiveSheet.UsedRange.AddressLocal
End If
lOne:
Set xR = Application.InputBox("Select the Input range:", "ExcelDemy", xT, , , , , 8)
If xR Is Nothing Then Exit Sub
If xR.Columns.Count > 1 Or xR.Areas.Count > 1 Then
MsgBox " Selected Multiple Columns ", vbInformation, "Exceldemy"
GoTo lOne
End If
xER = xR.Rows.Count + xR.Row
Application.ScreenUpdating = False
For p = xR.Rows.Count To 1 Step -1
If xR.Cells(p) = "Passed" Then
xR.Cells(p).EntireRow.Cut
Rows(xER).Insert Shift:=xlDown
End If
Next
Application.ScreenUpdating = True
End Sub

🔎 VBA Code Breakdown:

Segment 1:

Sub Move_Row_To_End()
Dim xR As Range
Dim xT As String
Dim xC As Range
Dim xER As Long
Dim p As Long
On Error Resume Next

Here, there is created a new sub named Move_Row_To_End. Then called 4 variables and commanded to go to the next line if found any error.


Segment 2:

If ActiveWindow.RangeSelection.Count > 1 Then
xT = ActiveWindow.RangeSelection.AddressLocal
Else
xT = ActiveSheet.UsedRange.AddressLocal
End If

Before running the code, if the number of selected cells is greater than 1 then the selected range will be the input range of the code. Else it will select all of the used cells as the input range.


Segment 3:

lOne:
Set xR = Application.InputBox("Select the Input range:", "ExcelDemy", xT, , , , , 8)

This line creates an input box named “Exceldemy” which will take the input of the cell range.


Segment 4:

If xR.Columns.Count > 1 Or xR.Areas.Count > 1 Then
MsgBox " Selected Multiple Columns ", vbInformation, "Exceldemy"
GoTo lOne
End If

These lines denote that if you select more than one column then it will give a message box saying “Selected Multiple Columns”.


Segment 5:

xER = xR.Rows.Count + xR.Row
Application.ScreenUpdating = False
For p = xR.Rows.Count To 1 Step -1
If xR.Cells(p) = "Passed" Then
xR.Cells(p).EntireRow.Cut
Rows(xER).Insert Shift:=xlDown
End If
Next
Application.ScreenUpdating = True
End Sub

This is the final part of the code. Here, For loop works to select the rows containing the given cell value “Passed” then cut the row and paste it to the bottom. After that, it will end the sub.

VBA to Move Row to Bottom in Excel If Cell Contains a Text

  • To run the code go to the top menu, press on the Run option, and here will open some other options and select the Run Sub/UserForm also you can simply press F5 to run the code.

Run VBA Code

  • Then, after running the code, there will open a pop-up window. Here, you will have to select the input range. For this dataset, the input range is E5:E20. Then Press OK.

VBA code run

  • As a result, you will see that the rows containing the specific text “Passed” have gone to the bottom of the dataset.

Move Row to Bottom in Excel If Cell Contains a Value - Output


2. Move Row to Bottom If a Cell Contains Greater Than a Number

When you want to move rows to the bottom that meet specific criteria, you have to change the previous slightly. Suppose, for the same dataset, you want to move rows that contain sales values of more than $4,000,000. Follow the steps below for this.

Steps:

  • First. create the same dataset in a new worksheet. Then, create a new module to insert the modified code using the same steps mentioned before.

Move Row to Bottom If a Cell Contains Greater Than a Number - Dataset

  • Now, a new “Module” window will appear. And Paste this VBA code into the box.
Sub Move_Row_To_End()
Dim xR As Range
Dim xT As String
Dim xC As Range
Dim xER As Long
Dim p As Long
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xT = ActiveWindow.RangeSelection.AddressLocal
Else
xT = ActiveSheet.UsedRange.AddressLocal
End If
lOne:
Set xR = Application.InputBox("Select the Input range:", "ExcelDemy", xT, , , , , 8)
If xR Is Nothing Then Exit Sub
If xR.Columns.Count > 1 Or xR.Areas.Count > 1 Then
MsgBox " Selected Multiple Columns ", vbInformation, "Exceldemy"
GoTo lOne
End If
xER = xR.Rows.Count + xR.Row
Application.ScreenUpdating = False
For p = xR.Rows.Count To 1 Step -1
If xR.Cells(p) > 4000000 Then
xR.Cells(p).EntireRow.Cut
Rows(xER).Insert Shift:=xlDown
End If
Next
Application.ScreenUpdating = True
End Sub

VBA to Move Row to Bottom If a Cell Contains Greater Than a Number - VBA Code

  • Then, run the code using the Run option,
  • After running the code, there will open a pop-up window. Here, you will have to select the input range. For this dataset, the input range is D5:D20. Then Press OK.

  • As a result, you will see that the rows containing sales greater than $4,000,000 have gone to the bottom of the dataset.

Move Row to Bottom If a Cell Contains Greater Than a Number - Output

Read More: How to Move Row to Another Sheet Based on Cell Value in Excel


Download Practice Workbook

You can download the practice workbook from here:


Conclusion

In this article, you have found how to move row to bottom if a cell contains a specific value in Excel. I hope you found this article helpful. Please, drop comments, suggestions, or queries if you have any in the comment section below.


Related Articles


<< Go Back to Move Rows | Rows in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Osman Goni Ridwan
Osman Goni Ridwan

OSMAN GONI RIDWAN is a marine engineer who loves exploring Excel and VBA programming. Programming, to him, is a time-saving tool for efficiently managing data, files, and internet tasks. He is proficient in Rhino3D, Maxsurf, C++, MS Office, AutoCAD, Excel, and VBA and goes beyond the basics. Armed with a B.Sc in Naval Architecture & Marine Engineering from BUET, OSMAN has transitioned into the role of a content developer. Specializing in creating technical content exclusively centered around Excel... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo