VBA: Copy and Paste to Different Tab

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hello Gurus,

Hope all is well.

I have two tabs namely Excel 1 and Excel 2. Excel 1 contains the data and I need to filter it to Copy+Paste in the Excel 2 tab based on the following criteria.

Copy and paste whole row if contains these:
1. Column B = Look for "Math"
2. Column C = Look for "UK"
3. Column E = Look for "Comments"

Note that some column have blank cells so filtering I think is the fastest way.


Thank you!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
VBA Code:
Sub myFunction()
  Dim lRow As Long
  lRow = Worksheets("Excel 1").Cells(Rows.Count, 2).End(xlUp).Row
  With Worksheets("Excel 1")
    For i = 1 to lRow
      If InStr(.Cells(i, 2).Value, "Math") * InStr(.Cells(i, 3).Value, "UK") * InStr(.Cells(i, 5).Value, "Comments") > 0 Then
        .Rows(i).Copy
        Worksheets("Excel 1").Cells(Rows.Count, 2).End(xlUp).Row.Rows(Worksheets("Excel 2").Cells(Rows.Count, 2).End(xlUp).Row + 1).EntireRow.Insert
      End If
    Next
  End With
End Sub
 
Upvote 0
VBA Code:
Sub myFunction()
  Dim lRow As Long
  lRow = Worksheets("Excel 1").Cells(Rows.Count, 2).End(xlUp).Row
  With Worksheets("Excel 1")
    For i = 1 to lRow
      If InStr(.Cells(i, 2).Value, "Math") * InStr(.Cells(i, 3).Value, "UK") * InStr(.Cells(i, 5).Value, "Comments") > 0 Then
        .Rows(i).Copy
        Worksheets("Excel 1").Cells(Rows.Count, 2).End(xlUp).Row.Rows(Worksheets("Excel 2").Cells(Rows.Count, 2).End(xlUp).Row + 1).EntireRow.Insert
      End If
    Next
  End With
End Sub
I tried the code but I'm getting a "Compile Error: Variable note defined" on the "i".
 
Upvote 0
Most probably you are using option explicit. Ok, try like this:
VBA Code:
Sub myFunction()
  Dim lRow As Long, i As Long
 
  With Worksheets("Excel 1")
    lRow = .Cells(Rows.Count, 2).End(xlUp).Row
    For i = 1 to lRow
      If InStr(.Cells(i, 2).Value, "Math") * InStr(.Cells(i, 3).Value, "UK") * InStr(.Cells(i, 5).Value, "Comments") > 0 Then
        .Rows(i).Copy
        Worksheets("Excel 2").Rows(Worksheets("Excel 2").Cells(Rows.Count, 2).End(xlUp).Row + 1).EntireRow.Insert
      End If
    Next
  End With
End Sub
 
Upvote 0
Thank you :)
Most probably you are using option explicit. Ok, try like this:
VBA Code:
Sub myFunction()
  Dim lRow As Long, i As Long
 
  With Worksheets("Excel 1")
    lRow = .Cells(Rows.Count, 2).End(xlUp).Row
    For i = 1 to lRow
      If InStr(.Cells(i, 2).Value, "Math") * InStr(.Cells(i, 3).Value, "UK") * InStr(.Cells(i, 5).Value, "Comments") > 0 Then
        .Rows(i).Copy
        Worksheets("Excel 2").Rows(Worksheets("Excel 2").Cells(Rows.Count, 2).End(xlUp).Row + 1).EntireRow.Insert
      End If
    Next
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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