Need Code Review to Copy all rows and paste to next sheet.

shahao

New Member
Joined
Feb 13, 2021
Messages
6
Office Version
  1. 2013
Platform
  1. Windows
I have an excel file with rows of more than 300,000, I need a macro code that copies all rows and paste them to a new sheet if any column has SPECIAL TEXT, the sheet has multiple columns from A-N,
I found a code that works on the same principle, but I don't know why It's not working in my case,
code is from this forum by (NORIE).

VBA Code:
Sub BankMove()
Dim strArray As Variant
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim NoRows As Long
Dim DestNoRows As Long
Dim I As Long
Dim J As Integer
Dim rngCells As Range
Dim rngFind As Range
Dim Found As Boolean
   
    strArray = Array("bank", "KLM", "firm")
   
    Set wsSource = ActiveSheet
   
    NoRows = wsSource.Range("A65536").End(xlUp).Row
    DestNoRows = 1
    Set wsDest = ActiveWorkbook.Worksheets.Add
       
    For I = 1 To NoRows
   
        Set rngCells = wsSource.Range("C" & I & ":F" & I)
        Found = False
        For J = 0 To UBound(strArray)
            Found = Found Or Not (rngCells.Find(strArray(J)) Is Nothing)
        Next J
       
        If Found Then
            rngCells.EntireRow.Copy wsDest.Range("A" & DestNoRows)
           
            DestNoRows = DestNoRows + 1
        End If
    Next I
End Sub


Please help me with this.
Thanks.
 

Attachments

  • Screenshot_6.png
    Screenshot_6.png
    66.8 KB · Views: 10

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I have an excel file with rows of more than 300,000
Then to start with you need to change
VBA Code:
NoRows = wsSource.Range("A65536").End(xlUp).Row
to
VBA Code:
NoRows = wsSource.Cells(Rows.count, "A").End(xlUp).Row
 
Upvote 0
What is wrong with the code supplied on OzGrid, that you said worked?
 
Upvote 0
What is wrong with the code supplied on OzGrid, that you said worked?
Well, I am not good with VBA, That code worked well with small files, but I have to play with million-plus rows, that code work with one string only, the code I mentioned above has a multiple string option.
The file he provided me works well with his code only not with my side. God knows why.
Looking for a better option now,
 
Upvote 0
It's going to work a lot better than the code you posted with that amount of data.
But your choice.
 
Upvote 0
Then to start with you need to change
VBA Code:
NoRows = wsSource.Range("A65536").End(xlUp).Row
to
VBA Code:
NoRows = wsSource.Cells(Rows.count, "A").End(xlUp).Row
Thanks man, God bless you. You sorted my headache, After craving for so long finally got some solid code.
With respect to you.
Thanks to you Norie.
 
Upvote 0
You're welcome, but I must say that I agree with Fluff that with that much data you are better off getting the array code amended.
 
Upvote 0
You're welcome, but I must say that I agree with Fluff that with that much data you are better off getting the array code amended.
Don't mind, can you please explain in simple words, I didn't FLUFF point.
Thanks.
 
Upvote 0
The array code that you have been given at Ozgrid is a lot faster than the looping Find code that you have posted above. With that much data the time difference will be very noticeable and so you are better off getting that code amended for multiple criteria.
 
Upvote 0
On a quick test with your data of ~10,000 rows, Your code takes 18 seconds & the array code takes 0.5 seconds.
Multiply that by 100 for over a million rows & you will see that there is going to be real difference in time.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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