SearchString,copy and paste in next sheet

Francois084

New Member
Joined
Jun 23, 2022
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Good day, All
I am using this code to look for specific Like value in a column then copy it over to next sheet and then remove the row
but when it goes to copy the next row it skips a row and start but the row below

Please Help.

Much thanks in advance

Sub SearchString()

Dim c As Range
Dim Source As Worksheet
Dim Target As Worksheet
Dim Target1 As Worksheet
Dim rowIterator As Long
Set Source = ActiveWorkbook.Worksheets("Master List")
Set Target = ActiveWorkbook.Worksheets("Group")

For Each c In Source.Range("E1:E" & Source.Cells(Rows.Count, "E").End(xlUp).Row)

If c Like "*FOR*" Then
c.EntireRow.copy
Target.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
c.EntireRow.Delete
ElseIf c Like "*+*" Then
c.EntireRow.copy
Target.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
c.EntireRow.Delete
End If
Next c

End Sub
 

Attachments

  • exa.png
    exa.png
    2.7 KB · Views: 10

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome to the Board!

There is always danger when trying to loop through a range, but then you are either deleting or inserting rows into that range as you are looping through it.
This can cause certain rows to be skipped (as deleting a row moves all the rows below that up one) or certain rows to be hit twice (as inserting a row moves all rows down one).

It is much better to loop through the range backwards to avoid those issues.

So you can find the last row with data in the column you are working with, and then loop through them like this:
VBA Code:
For r = lastRow to 1 Step -1
and then refer to your ranges within your loop like this:
VBA Code:
Cells(r, "E")
or
VBA Code:
Range("E" & r)
 
Upvote 0
Thank you
Sorry to ask!
Could you please explain more or give an example or where r should be.
I only start VBA this week
 
Upvote 0
Maybe something like this (untested):
VBA Code:
Sub SearchString()

Dim lr As Long
Dim r As Long
Dim c As Range
Dim Source As Worksheet
Dim Target As Worksheet

Set Source = ActiveWorkbook.Worksheets("Master List")
Set Target = ActiveWorkbook.Worksheets("Group")

'   Find last row in column E on Source sheet
    lr = Source.Cells(Rows.Count, "E").End(xlUp).Row

'   Loop through each row in column E backwards
    For r = lr To 1 Step -1
'       Build "c" range
        Set c = Source.Range("E" & r)
'       Proceed with rest of your code
        If c Like "*FOR*" Then
            c.EntireRow.Copy
            Target.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
            c.EntireRow.Delete
        ElseIf c Like "*+*" Then
            c.EntireRow.Copy
            Target.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
            c.EntireRow.Delete
        End If
    Next r

End Sub
Note that I documented the code to explain what is going on in the steps I created.
 
Upvote 0
Solution
You are welcome.

I hope it makes sense the issue with trying to loop through a range going forward as you are messing with the size of the range.
If you weren't deleting any rows from your original range, your loop would work fine, as it you had it originally written.
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,118
Members
449,066
Latest member
Andyg666

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