memory leak?

gdesreu

Active Member
Joined
Jul 30, 2012
Messages
318
I've been using the following code for a few years to flip spreadsheet data around without issue. Recently however I received a large data set (about 5X larger than those I typically work with and the macro eventually runs out of resources and stops. Typically this runs instantly. I've tried it with screenupdating off and on and it has the same result along with various methods to clear the clipboard after the copy past sequences, they all have the same result. Obviously I have something wrong with my code. I can cut the data set in half and run it twice and it will work but I would prefer to know what is causing the out of resources crash. After a certain number of loops you can watch it get progressively slower each loop until eventually it crashes. Can someone tell me what I am doing wrong with the code below? Thanks
Code:
Sub flip()
'paste analytes
Do While Range("W2").Value > 0 
Dim LR
Range("s21:s73").Select 
    Selection.Copy
LR = Range("s" & Rows.Count).End(xlUp).Offset(1, 0).Select 
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=False
 
    Range("w21:y73").Select
    Selection.Copy
    Range("s21").End(xlDown).Offset(-52, 1).Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=False
       
   Range("w2:w19").Select
    Selection.Copy
HDrng1 = Range("a21").End(xlDown).Offset(1, 0).Row 
HDrng2 = Range("s21").End(xlDown).Offset(0, -1).Row 
HDrange = "(a" & HDrng1 & ":" & "r" & HDrng2 & ")" 
Range(HDrange).Select 
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=True
       Columns("w:y").Select 
    Selection.Delete Shift:=xlToLeft
    Range("w2").Select
    Loop
End Sub
 
To update the problem and solution. I fixed the issue after closer examination of the file. The file had numerous cells formatted with different colors that I didn't catch at first and even if I did it has never really caused an issue before. It also had numerous cells with different txt formats. Once I removed the formats, my code ran fine on the entire file. (although it took a while) . The code samples above failed as mine did but are more efficient and simpler to use than mine so I plan on adapting these variations. I'm still scratching my head as to why pasting values and deleting the columns with formatted cells crashes excel after a number of loops. I would expect it to get faster as it deletes more records and subsequent formatting but that is not the case. Hopefully someone is about to explain this is detail for future reference so I can better understand it and not do it again but if not, that was what solved the problem in this case.
Thanks guys.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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