How to Copy a Row Using Excel VBA If the Cell Value Matches (2 Methods)

Dataset Overview

We’ll use the sample dataset below to demonstrate two methods.

Suitable Methods to Copy Row If Cell Value Matches with Excel VBA


Method 1 – Copy Row to Another Sheet If One Cell Value Matches Using VBA in Excel

From the data set in Dataset1, we want to differentiate the sold items and show them in worksheet Sold1.

Step 1 – Create a Module

  • Press Alt + F11 to start the VBA Macro.
  • Click on the Insert.
  • Select the Module option.

Suitable Methods to Copy Row If Cell Value Matches with Excel VBA

Step 2 – Paste the VBA Codes

  • Enter the following VBA code:
Sub CopyRow()
'Declare variables
    Dim DataRg As Range
    Dim DataCell As Range
    Dim P As Long
    Dim J As Long
    Dim I As Long
'Set variables
    P = Worksheets("Dataset1").UsedRange.Rows.Count
    Q = Worksheets("Sold1").UsedRange.Rows.Count
''Type If condition to relate the variables I and Q
    If I = 1 Then
    If Application.WorksheetFunction.CountA(Worksheets("Sold1").UsedRange) = 0 Then Q = 0
    End If
'Set range for Dataset1
    Set DataRg = Worksheets("Dataset1").Range("E5:E11" & P)
    On Error Resume Next
    Application.ScreenUpdating = False
'Apply the For loop
    For I = 1 To DataRg.Count
'Set Condition for "Sold" value
        If CStr(DataRg(I).Value) = "Sold" Then
'Apply command to copy cells
            DataRg(I).EntireRow.Copy Destination:=Worksheets("Sold1").Range("A" & Q + 1)
            Q = Q + 1
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Suitable Methods to Copy Row If Cell Value Matches with Excel VBA

Step 3 – Run the Program

  • Save the program and press F5 to run it.

Suitable Methods to Copy Row If Cell Value Matches with Excel VBA

Read More: Excel VBA: Copy Cell Value and Paste to Another Cell


Method 2 – Copy Row to Multiple Sheets for Multiple Criteria at Once If Cell Value Matches Using VBA in Excel

It’s a very important section, as you can learn to copy and move cells based on the matched values in multiple sheets simultaneously. We have a dataset in worksheet Dataset2 and we want to place the sold products in the worksheet Sold2 and the unsold products in the worksheet Unsold2. Follow the outlined procedure to accomplish the task.

Suitable Methods to Copy Row If Cell Value Matches with Excel VBA

Step 1 – Select a New Module

  • Insert a new Module.

Suitable Methods to Copy Row If Cell Value Matches with Excel VBA

Step 2 – Paste the VBA code

  • Copy and paste the following VBA code into the Module.
Sub CopyRow2()
'Declare variables
    Dim sheetNo1 As Worksheet
    Dim sheetNo2 As Worksheet
    Dim sheetNo3 As Worksheet
    Dim FinalRow As Long
    Dim Cell As Range
'Set variables
    Set sheetNo1 = Sheets("Dataset2")
    Set sheetNo2 = Sheets("Sold2")
    Set sheetNo3 = Sheets("Unsold2")
'Type a command to select the entire row
    Selection.EntireRow.Select
' Define destination sheets to move row
    FinalRow1 = sheetNo1.Range("A" & sheetNo1.Rows.Count).End(xlUp).Row
    FinalRow2 = sheetNo2.Range("A" & sheetNo2.Rows.Count).End(xlUp).Row
    FinalRow3 = sheetNo3.Range("A" & sheetNo3.Rows.Count).End(xlUp).Row
    With sheetNo1
'Apply loop for column E until last cell with value
    For Each Cell In .Range("E1:E" & .Cells(.Rows.Count, "E").End(xlUp).Row)
'Apply condition to match the "Sold" value
        If Cell.Value = "Sold" Then
'Command to Copy and move to a destination Sheet "Sold2"
            .Rows(Cell.Row).Copy Destination:=sheetNo2.Rows(FinalRow2 + 1)
            FinalRow2 = FinalRow2 + 1
'Apply condition to match the "Unsold" value
        ElseIf Cell.Value = "Unsold" Then
'Command to Copy and move to a destination Sheet "Unsold2"
            .Rows(Cell.Row).Copy Destination:=sheetNo3.Rows(FinalRow3 + 1)
             FinalRow3 = FinalRow3 + 1
        End If
     Next Cell
    End With
End Sub

Suitable Methods to Copy Row If Cell Value Matches with Excel VBA

Step 3 – Run the Program

  • Save the program and press F5 to run it.
  • As a result, the sold products will be placed in the worksheet Sold2, and the unsold products will appear in the worksheet Unsold2.

Suitable Methods to Copy Row If Cell Value Matches with Excel VBA

Sample Data

Notes.  If you want to move entire rows from the source data set, follow the steps below.

Step 1 – Add the Following VBA Codes

  • Insert the VBA code shown in the image below along with the existing code. Your final code should look like this:
Sub CopyRow4()
'Declare variables
    Dim sheetNo1 As Worksheet
    Dim sheetNo2 As Worksheet
    Dim sheetNo3 As Worksheet
    Dim FinalRow As Long
    Dim Cell As Range
'Set variables
    Set sheetNo1 = Sheets("Dataset2")
    Set sheetNo2 = Sheets("Sold2")
    Set sheetNo3 = Sheets("Unsold2")
'Type a command to select the entire row
    Selection.EntireRow.Select
' Define destination sheets to move row
    FinalRow1 = sheetNo1.Range("A" & sheetNo1.Rows.Count).End(xlUp).Row
    FinalRow2 = sheetNo2.Range("A" & sheetNo2.Rows.Count).End(xlUp).Row
    FinalRow3 = sheetNo3.Range("A" & sheetNo3.Rows.Count).End(xlUp).Row
    With sheetNo1
'Apply loop for column E until last cell with value
    For Each Cell In .Range("E1:E" & .Cells(.Rows.Count, "E").End(xlUp).Row)
'Apply condition to match the "Sold" value
        If Cell.Value = "Sold" Then
'Command to Copy and move to a destination Sheet "Sold2"
            .Rows(Cell.Row).Copy Destination:=sheetNo2.Rows(FinalRow2 + 1)            
            .Rows(Cell.Row).Delete            
            FinalRow2 = FinalRow2 + 1
'Apply condition to match the "Unsold" value
        ElseIf Cell.Value = "Unsold" Then
'Command to Copy and move to a destination Sheet "Unsold2"
            .Rows(Cell.Row).Copy Destination:=sheetNo3.Rows(FinalRow3 + 1)            
            .Rows(Cell.Row).Delete          
             FinalRow3 = FinalRow3 + 1
        End If
     Next Cell
    End With
  End Sub

Sample Data

Step 2 – Run the Codes

  • Click on the Save icon and press F5 to run the program.
  • As a result, your source dataset will be removed as the figure shows below.

Sample Data

  • The Sold values will be in the Sold2 sheet.

Sample Data

  • The unsold values will be in the Unsold2 sheet.

Sample Data


Download Practice Workbook

You can download the practice workbook from here:


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Bhubon Costa
Bhubon Costa

Bhubon Costa, B.Sc. in Naval Architecture & Marine Engineering from Bangladesh University of Engineering & Technology, has worked with the ExcelDemy since 2021. Currently, he has been working as a reviewer. Notably, he has written over 90 articles and led several VBA content development teams. He has a great passion for the fields of data analytics and data science. His areas of expertise include Excel VBA, Power Query, Pivot Table, Power BI, MySQL, PostgreSQL, machine learning, and Python... Read Full Bio

6 Comments
  1. Hello,

    I’m trying to apply this to many Sheets will many Different Cell types.
    Would you be able to help with this?
    This is what I have so far, but I’m getting a “subscript out of range” error.

    Sub MoveRowBasedOnCellValue()
    ‘Declare Variables
    Dim MAIN As Worksheet
    Dim Project As Worksheet
    Dim Claims As Worksheet
    Dim TaM As Worksheet
    Dim QUOTED As Worksheet
    Dim PM As Worksheet
    Dim FinalRow As Long
    Dim Cell As Range

    ‘Set Variables
    Set MAIN = Sheets(“Dataset2”)
    Set Claims = Sheets(“Claims”)
    Set Project = Sheets(“Project”)
    Set TaM = Sheets(“TaM”)
    Set PM = Sheets(“PM”)
    Set QUOTED = Sheets(“QUOTED”)

    ‘Type a Command to select the entire row
    Selection.EntireRow.Select

    ‘Define destination sheets to move row
    FinalRow1 = MAIN.Range(“A” & MAIN.Rows.Count).End(xlUp).Row
    FinalRow2 = Claims.Range(“A” & Claims.Rows.Count).End(xlUp).Row
    FinalRow3 = Project.Range(“A” & Project.Rows.Count).End(xlUp).Row
    FinalRow4 = TaM.Range(“A” & TaM.Rows.Count).End(xlUp).Row
    FinalRow5 = QUOTED.Range(“A” & QUOTED.Rows.Count).End(xlUp).Row
    FinalRow6 = PM.Range(“A” & PM.Rows.Count).End(xlUp).Row
    With MAIN

    ‘Apply loop for column F until last cell with value
    For Each Cell In .Range(“F1:F” & .Cells(.Rows.Count, “F”).End(xlUp).Row)

    ‘Apply condition to match the “CLAIMS” value
    If Cell.Value = “CLAIMS” Then
    ‘Command to Copy and move to a destination Sheet “CLAIMS”
    .Rows(Cell.Row).Copy Destination:=Claims.Rows(FinalRow2 + 1)
    FinalRow2 = FinalRow2 + 1
    ‘Apply condition to match the “PROJECT” value
    ElseIf Cell.Value = “EQUIPMENT PROJECT” Then
    ‘Command to Copy and move to a destination Sheet “PROJECT”
    .Rows(Cell.Row).Copy Destination:=Project.Rows(FinalRow3 + 1)
    FinalRow3 = FinalRow3 + 1
    ‘Apply condition to match the “PROJECT” value
    ElseIf Cell.Value = “CONTRACTING PROJECT” Then
    ‘Command to Copy and move to a destination Sheet “PROJECT”
    .Rows(Cell.Row).Copy Destination:=Project.Rows(FinalRow3 + 1)
    FinalRow3 = FinalRow3 + 1
    ‘Apply condition to match the “TaM” value
    ElseIf Cell.Value = “T&M” Then
    ‘Command to Copy and move to a destination Sheet “TaM”
    .Rows(Cell.Row).Copy Destination:=TaM.Rows(FinalRow4 + 1)
    FinalRow4 = FinalRow4 + 1
    ‘Apply condition to match the “Quoted” value
    ElseIf Cell.Value = “QUOTED” Then
    ‘Command to Copy and move to a destination Sheet “PROJECT”
    .Rows(Cell.Row).Copy Destination:=QUOTED.Rows(FinalRow5 + 1)
    FinalRow5 = FinalRow5 + 1
    ‘Apply condition to match the “SVC AGR” value
    ElseIf Cell.Value = “SVC AGR” Then
    ‘Command to Copy and move to a destination Sheet “PM”
    .Rows(Cell.Row).Copy Destination:=PM.Rows(FinalRow6 + 1)
    FinalRow6 = FinalRow6 + 1
    End If
    Next Cell
    End With
    End Sub

    Any help would be appreciated.

    Thank you,
    Andrea

    • First of all, take cordial greetings from the ExcelDemy team. There could be some possible reasons due to the issue:
      The inverted commas are not properly inserted in your code as they should be in VBA script.
      The Sheet Names might not be inserted in VBA exactly as they are in the Excel file.
      However, we have created an Excel file precisely in accordance with your requirements. We can send the Excel file if you provide your email address.
      Otherwise, you can copy the codes below that we have used to make the Excel file with the right syntax. Please continue to provide us with feedback; ExcelDemy always welcomes your concerns.

      Sub MoveRowBasedOnCellValue()
      ‘Declare Variables
      Dim MAIN As Worksheet
      Dim Project As Worksheet
      Dim Claims As Worksheet
      Dim TaM As Worksheet
      Dim QUOTED As Worksheet
      Dim PM As Worksheet
      Dim FinalRow As Long
      Dim Cell As Range

      ‘Set Variables
      Set MAIN = Sheets(“Dataset2”)
      Set Claims = Sheets(“CLAIMS”)
      Set Project = Sheets(“Project”)
      Set TaM = Sheets(“TaM”)
      Set PM = Sheets(“PM”)
      Set QUOTED = Sheets(“QUOTED”)

      ‘Type a Command to select the entire row
      Selection.EntireRow.Select

      ‘Define destination sheets to move row
      FinalRow1 = MAIN.Range(“A” & MAIN.Rows.Count).End(xlUp).Row
      FinalRow2 = Claims.Range(“A” & Claims.Rows.Count).End(xlUp).Row
      FinalRow3 = Project.Range(“A” & Project.Rows.Count).End(xlUp).Row
      FinalRow4 = TaM.Range(“A” & TaM.Rows.Count).End(xlUp).Row
      FinalRow5 = QUOTED.Range(“A” & QUOTED.Rows.Count).End(xlUp).Row
      FinalRow6 = PM.Range(“A” & PM.Rows.Count).End(xlUp).Row
      With MAIN

      ‘Apply loop for column F until last cell with value
      For Each Cell In .Range(“F1:F” & .Cells(.Rows.Count, “F”).End(xlUp).Row)

      ‘Apply condition to match the “CLAIMS” value
      If Cell.Value = “CLAIMS” Then

      ‘Command to Copy and move to a destination Sheet “CLAIMS”
      .Rows(Cell.Row).Copy Destination:=Claims.Rows(FinalRow2 + 1)
      FinalRow2 = FinalRow2 + 1

      ‘Apply condition to match the “PROJECT” value
      ElseIf Cell.Value = “EQUIPMENT PROJECT” Then

      ‘Command to Copy and move to a destination Sheet “PROJECT”
      .Rows(Cell.Row).Copy Destination:=Project.Rows(FinalRow3 + 1)
      FinalRow3 = FinalRow3 + 1

      ‘Apply condition to match the “PROJECT” value
      ElseIf Cell.Value = “CONTRACTING PROJECT” Then

      ‘Command to Copy and move to a destination Sheet “PROJECT”
      .Rows(Cell.Row).Copy Destination:=Project.Rows(FinalRow3 + 1)
      FinalRow3 = FinalRow3 + 1

      ‘Apply condition to match the “TaM” value
      ElseIf Cell.Value = “T&M” Then

      ‘Command to Copy and move to a destination Sheet “TaM”
      .Rows(Cell.Row).Copy Destination:=TaM.Rows(FinalRow4 + 1)
      FinalRow4 = FinalRow4 + 1

      ‘Apply condition to match the “Quoted” value
      ElseIf Cell.Value = “QUOTED” Then

      ‘Command to Copy and move to a destination Sheet “QUOTED”
      .Rows(Cell.Row).Copy Destination:=QUOTED.Rows(FinalRow5 + 1)
      FinalRow5 = FinalRow5 + 1

      ‘Apply condition to match the “SVC AGR” value
      ElseIf Cell.Value = “SVC AGR” Then

      ‘Command to Copy and move to a destination Sheet “PM”
      .Rows(Cell.Row).Copy Destination:=PM.Rows(FinalRow6 + 1)
      FinalRow6 = FinalRow6 + 1
      End If

      Next Cell

      End With

      End Sub

  2. This has really helped me, but I was wondering if it is possible to amend the formula slightly to paste values instead of a straight paste?

    Thanks

    Laura

  3. Hello Laura,
    I hope you are doing well. If you want to use a VBA code that can only paste values instead of a straight paste, you may use the following one.

    Sub Paste_Row_Values()
    a = Worksheets(“Dataset2”).Cells(Rows.Count, 1).End(xlUp).Row
    For i = 2 To a
    If Worksheets(“Dataset2”).Cells(i, 4).Value = “CLAIMS” Then
    Worksheets(“Dataset2”).Rows(i).Copy
    Worksheets(“CLAIMS”).Activate
    b = Worksheets(“CLAIMS”).Cells(Rows.Count, 1).End(xlUp).Row
    Worksheets(“CLAIMS”).Cells(b + 1, 1).Select
    ActiveCell.PasteSpecial Paste:=xlPasteValues
    Worksheets(“Dataset2”).Activate

    End If
    Next
    Application.CutCopyMode = False
    ThisWorkbook.Worksheets(“Dataset2”).Cells(1, 1).Select
    End Sub

    I think you get your required solution. By using this code, you can paste the row values without any format. If you have any further queries, feel free to connect us.

  4. It is working correctly, i got what i want, thanks to you

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo