How to Move a Row to the Bottom If a Cell Contains a specific Value in Excel – 2 Methods

The sample dataset contains customer name, product brand name, sales amount, and target status. Some cells of the target column display “ Passed” . To move these rows to the bottom:

Excel Move Row to Bottom If Cell Contains a Value

 


Method 1 – Move Row to Bottom If Cell Contains a Specific Text

Steps:

  • Go to the Developer tab and click Visual Basic.

You can press ALT + F11 to open the Microsoft Visual Basic for Applications window.

Microsoft Visual Basic for Applications

  • Select “Insert” and choose “Module’”.

Insert Module

  • Enter this VBA code:
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

The Move_Row_To_End new sub is created.  4 variables are declared and commanded to move to the next line if an error is found.


 Segment 2:

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

If the number of selected cells is greater than 1, the selected range will be the input range of the code. Otherwise, it will select all 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: “Exceldemy” to 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

If you select more than one column, a message box will display “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

A For loop selects the rows containing the  “Passed”, cut the row and paste it at the bottom of the sheet. The sub ends.

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

  • Click Run and select the Run Sub/UserForm or press F5 to run the code.

Run VBA Code

  • In the pop-up window, enter the input range. Here, E5:E20.
  • Click OK.

VBA code run

Rows containing “Passed” are at the bottom of the dataset.

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


Method 2 – Move a Row to Bottom If a Cell Contains a Number Greater Than a specified Number

To move rows that contain sales values greater than $4,000,000 to the bottom of the dataset:

Steps:

  • Open the dataset below in a new worksheet.
  • Insert a new module:

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

  • Enter this VBA code:
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

  • Click Run.
  • In the pop-up window, enter the input range. Here, D5:D20.
  • Click OK.

Rows containing sales greater than $4,000,000 are at 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

Download the practice workbook.


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