Memory issues with macros

paul_taylor

New Member
Joined
Feb 15, 2011
Messages
33
I ran a macro to loop through all the sheets (about 10) in a workbook and clear out a certain range. The file grew from 200K to 5 MB. What's going on here? Can't figure it out.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Sub Macro3()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim sheetloop As Long


sheetloop = 4
Do While sheetloop <= 14

Select Case sheetloop
Case 4, 5, 6, 8, 9, 10, 11, 13, 14
Sheets(sheetloop).Activate
Range("A5:z10000").Select
Selection.Clear


Case 7
Sheets(sheetloop).Activate
Range("A6:z10000").Select
Selection.Clear

Case 12

Sheets(sheetloop).Activate
Range("A11:z10000").Select
Selection.Clear


End Select

sheetloop = sheetloop + 1
Loop






Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True


End Sub
 
Upvote 0
Ok, by clearing the values between row 20 & 10000 you have essentially made them active and this automatically increases file size, had you used delete (which is slightly less efficient that clear) you probably wouldn't have had this problem.

There is also no need to activate the sheets to do this or to select the cells.

Try this though, I have assumed Column A has contiguous data and have used this to define last row, if not change to suit.

Code:
Sub Macro3()
    
    Dim sheetloop As Long
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With

    sheetloop = 4
    For sheetloop = 4 To 14
        Select Case sheetloop
            Case 4, 5, 6, 8, 9, 10, 11, 13, 14
                With Sheets(sheetloop)
                    .Range("A5:Z" & .Range("A" & Rows.Count).End(xlUp).Row).Clear
                End With
            Case 7
                With Sheets(sheetloop)
                    .Range("A6:Z" & .Range("A" & Rows.Count).End(xlUp).Row).Clear
                End With
            Case 12
                With Sheets(sheetloop)
                    .Range("A11:Z" & .Range("A" & Rows.Count).End(xlUp).Row).Clear
                End With
        End Select
    Loop
    
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

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