Nested VBA loop crashes Excel

daggg

New Member
Joined
Aug 10, 2016
Messages
18
Public Sub abc123()
Dim EndRow As Long, I As Integer, J As Long, k As Long
I = 1
EndRow = Cells(Rows.Count, I).End(-4162).Row
Application.ScreenUpdating = False
For k = 1 To EndRow
For J = 1 To EndRow
If Cells(k, I + 8) = Cells(J, I) Then
Cells(J, I).Delete
Cells(J, I + 1).Delete
Cells(J, I + 2).Delete
Cells(J, I + 3).Delete
Cells(J, I + 4).Delete
Cells(J, I + 5).Delete
Cells(J, I + 6).Delete
Cells(J, I + 7).Delete
End If
Next
Next
Application.ScreenUpdating = True
End Sub


[FONT=&quot]The macro works on the sample but when I input my data of up to 20,000 rows the spreadsheet freezes and crashes excel.[/FONT]<img style="box-sizing: border-box; vertical-align: middle; border: 0px; color: rgb(73, 73, 73); font-family: "Gotham SSm", Helvetica, Arial, sans-serif; background-color: rgb(250, 250, 250);">
 

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

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,648
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
Re: Heeeeelp, please! :)

Do you actually need to delete the cells or simply clear the contents ???
What are you trying to achieve with this code ??
 
Upvote 0

daggg

New Member
Joined
Aug 10, 2016
Messages
18
Re: Heeeeelp, please! :)

Delete them...But I would like to have both options... Let's try first this one... Thank you very much for your answer...
 
Upvote 0

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,648
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
ADVERTISEMENT
Re: Heeeeelp, please! :)

Maybe this then...
Code:
Public Sub abc123()
Dim EndRow As Long, I As Integer, J As Long, k As Long
I = 1
EndRow = Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For k = 1 To EndRow
    For J = 1 To EndRow
        If Cells(k, I + 8) = Cells(J, I) Then
            Range(Cells(J, I), Cells(J, I + 7)).ClearContents
        End If
    Next J
Next k
Application.ScreenUpdating = True
End Sub
 
Upvote 0

daggg

New Member
Joined
Aug 10, 2016
Messages
18
Re: Heeeeelp, please! :)

Thank you very much, but again there is that Not Responding error message...
 
Upvote 0

daggg

New Member
Joined
Aug 10, 2016
Messages
18
ADVERTISEMENT
Re: Heeeeelp, please! :)

20000 makes the problem...
 
Upvote 0

BQardi

Active Member
Joined
Aug 30, 2016
Messages
483
Re: Heeeeelp, please! :)

Well, you are looping 20,000 rows 20,000 times. A total of 400,000,000 runs...
 
Upvote 0

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,648
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
Re: Heeeeelp, please! :)

as asked earlier.....

Code:
What are you trying to achieve with this code ??
An explanation of what you are trying to do would be far more helpful, than providing inefficient and buggy code ...
 
Upvote 0

Forum statistics

Threads
1,195,600
Messages
6,010,651
Members
441,558
Latest member
lambierules

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
Top