Vba: cut and paste entire rows (out of memory)

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I'm dealing with a big amount of datas, about 40k rows.

I need to cut and paste entire rows from a sheet to another, if conditions are not checked.

Example: cut and copy in the sheet Beta if in column A of sheet Alpha <> 36 characters

Assuming - but I'm not sure - that the following code is correct, I bump into a Run-time error '7': Out of memory

Code:
    Dim i As Long
    
    For i = Sheets("Alpha").Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
        If Len(Cells.Value) <> 36 Then
            Sheets("Alpha").Range("A" & i).Copy Sheets("Beta").Cells(Rows.Count, 1).End(xlUp).Offset(1)
            Cells(i, 1).EntireRow.Delete
        End If
    Next

How can I manage this deadlock?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Maybe try a different approach.
Like, create a helper column that returns the length of each row.
Then, use Filters on this helper field to identify and move the records you want.
You can use Filters within VBA too, if desired.
 
Upvote 0
Assuming you have a header row in row1 with data starting row 2, try
Code:
Sub FltrDel()
   With Sheets("Alpha")
      If .AutoFilterMode Then .AutoFilterMode = False
      Range("A1").AutoFilter 10, "<>" & String(36, Chr(63))
      .UsedRange.Offset(1).SpecialCells(xlVisible).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
With the following I've create the column (J): the row with "KO" in column J must be transfered from sheet Alpha to sheet Beta.
I'm going to try.

Code:
For Each c In Range("A2:A" & lr)
    If Len(c.Value) <> 36 Then c.Offset(0, 10).Value = "KO"
Next
 
Upvote 0
Loops are extremely slow and inefficient, so I don't know if that is going to be any better.
Consider using one of the Filter options we suggested.
 
Upvote 0
Uhm...
so, I'm going to try filtering the new helper column K.
 
Last edited:
Upvote 0
You don't need a loop to populate that column (avoid using loops whenever possible, that is kind of what got you in this mess;)).
Just do it like this:
Code:
    Range("K2:K" & lr).FormulaR1C1 = "=IF(LEN(RC[-10])<>36,""KO"","""")"
 
Upvote 0
Assuming you have a header row in row1 with data starting row 2, try
Code:
Sub FltrDel()
   With Sheets("Alpha")
      If .AutoFilterMode Then .AutoFilterMode = False
      Range("A1").AutoFilter 10, "<>" & String(36, Chr(63))
      .UsedRange.Offset(1).SpecialCells(xlVisible).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
Brilliant!
Unfortunately, people could not understand its beauty/compactness...
Resizing as
Code:
.UsedRange.Resize(.UsedRange.rows.count - 1).Offset(1).SpecialCells(xlVisible).EntireRow.Delete
will avoid deleting an extra empty row, but this does not matter too much. I use VBA from some years now, but I could not think of that trick of building a string of question marks (to replace each existing character) as criteria.
Really brilliant, at least, on my taste...
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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