Help fix the code Error Out of memory when Variable A >=600,000

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hi guys!. I have used the code below to filter data from Sheet data , At variable A (cell M5 ) when the value is >=560,000, the code still runs 4 5 times, then runs again, shows Out of memory error, And gives an error at the line
sArr = Sheets("aaa").Range("AA1:AQ" & a).Value . Ask people to correct the code or switch to a new algorithm so that there is no Out of memory error. I sincerely thank

VBA Code:
Sub abcd()
Application.ScreenUpdating = False
On Error Resume Next
Dim sArr(), dArr(), I As Long, K As Long, R As Long, Col As Long, a As Long, Dk1 As String, xx As Long
a = Range("m5").Value
Dk1 = Range("a2").Value
[B]sArr = Sheets("aaa").Range("AA1:AQ" & a).Value[/B]
R = UBound(sArr)
ReDim dArr(1 To R, 1 To 19)
For I = 1 To R
    If (UCase(sArr(I, 9)) = UCase(Dk1) Or UCase(sArr(I, 9)) Like UCase(Dk1) & "-*") Then
        K = K + 1
        dArr(K, 1) = sArr(I, 2) & " x " & sArr(I, 9)
            For Col = 2 To 18
                dArr(K, Col) = sArr(I, Col - 1)
            Next Col
        dArr(K, 19) = sArr(I, 2) & " x " & sArr(I, 9)
    End If
Next I
Range("R2:BC260000").ClearContents
Range("R2").Resize(K, 19) = dArr
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I have no problem with 600,000 even tested with a million and it works.
2 recommendations: remove the On Error statement and at the end of the code Save the workbook to free up memory.

Try:

Rich (BB code):
Sub abcd()
  Dim sArr(), dArr()
  Dim i As Long, K As Long, R As Long, Col As Long, a As Long
  Dim Dk1 As String
  
  Application.ScreenUpdating = False
  a = Range("m5").Value
  Dk1 = Range("a2").Value
  sArr = Sheets("aaa").Range("AA1:AQ" & a).Value
  R = UBound(sArr)
  ReDim dArr(1 To R, 1 To 19)
  For i = 1 To R
    If (UCase(sArr(i, 9)) = UCase(Dk1) Or UCase(sArr(i, 9)) Like UCase(Dk1) & "-*") Then
      K = K + 1
      dArr(K, 1) = sArr(i, 2) & " x " & sArr(i, 9)
      For Col = 2 To 18
        dArr(K, Col) = sArr(i, Col - 1)
      Next Col
      dArr(K, 19) = sArr(i, 2) & " x " & sArr(i, 9)
    End If
  Next i
  
  Range("R2:BC260000").ClearContents
  Range("R2").Resize(K, 19) = dArr
  ThisWorkbook.Save
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,247
Messages
6,123,853
Members
449,129
Latest member
krishnamadison

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