How to avoid running out of memory while running VBA

yellow_orange

New Member
Joined
Feb 27, 2010
Messages
49
I created a sample size calculator macro. My problem is that when I run the macro to calculate sample size for a dozen times. Error msgbox just pops out the message out of memory. The macro is running smoothly and gives me accurate results.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Rewrite the macro so it doesn't waste memory.






















We can help you if you will share it with us.
 
Upvote 0
Share the code :)

In general
Code:
'Avoid
Object.Select
Selection.Activity
'use
Object.Activity
'same for 
Object.Activate
ChildObect.Acitcty
'sue
Object.Child.Activity
'Unless you intend that the ActiveObject be in view in the Excel Window after all Process run out.
The use of as below is recommended for all but the most simple macros
Code:
Sub myProcedure()
Application. ScreeenUpdate = False
Application.EnableEvents = False
My Code here
Application. ScreeenUpdate = True
Application.EnableEvents = True
End Sub
Prevent memory Creep. kill objects when they are no longer needed. End Sub kills most objects, not all.
Code:
'Dim myVar As Range, Sheet, Workbook, etc Will Die at End Sub.
'Dim MyVar As New Workbook, Window, Worksheet, Etc Won't.
Dim myVar As New Workbook 'Won't Die at End Sub
'Code here
Set MyVar = Nothing
End Sub
Avoid passing parameters "ByVal," they take more memory than "ByRef."

Count the Dots in loops
Code:
For L = 1 to 100
   Sheets("sheet1").Range("A" & L &"").Value = L * x
Next L
' 200 Dots
 
With Sheets("sheet1")
For L = 1 to 100
   Range("A" & L &"").Value = L * x
Next L
' 100 Dots
 
Upvote 0
Dear all,
I also have the problem with the " out of memory". The data include about 240000 rows and 55 columns.
Time (hr)KPChamM348000UPrekDamNekLuongsp40dsp35dsp37dTBAS5TBAS24TLT82KohKhelB332000B272000TRAB352521TLT1222525274TAFP4
02.2091.3261.4160.9222.1232.5120.6353.1510.5010.9971.1561.3120.7761.1620.861.0250.9410.9030.486
12.2271.3171.4071.0512.1232.5220.6343.1510.5011.0071.1651.30.7941.1610.9981.0790.9260.8940.604
22.2431.3211.41.1872.1222.5320.6443.1510.5011.0111.1661.2970.8931.2161.0831.190.9350.9110.727
32.2581.3491.4011.2242.1222.5410.6593.1510.5011.0291.1641.3090.9671.3081.1271.2730.9750.9550.82
42.271.3931.4151.1682.1222.5510.6773.1510.5011.0481.171.3421.0231.3471.0931.2751.0341.0140.829
52.2821.4181.4411.0962.1222.560.6943.1510.5011.0651.1841.3761.0051.3241.0331.221.0761.050.723
62.2961.421.4611.0362.1222.570.713.1510.5011.081.2021.3920.981.2750.9621.161.0821.0520.61
72.3121.4131.4640.9912.1212.5790.723.1510.5011.0921.2161.3910.9511.2250.8961.1141.0651.0360.538
82.331.4011.4590.9592.1212.5880.7233.1510.5011.0991.2281.3830.9221.1810.8391.0791.0381.010.487
92.3471.3891.4490.9312.1212.5970.7183.1510.5011.1021.2361.3740.8961.1430.7851.051.0070.980.443
102.3621.3781.440.9012.1212.6060.7093.1510.5011.0991.2391.3650.8741.1080.7321.0220.9750.9470.402
112.3751.3691.4320.8682.122.6150.6973.1510.5011.1011.2391.3560.8551.0730.6810.9910.9420.9130.36

<tbody>
</tbody>

I am writing small code to convert it into daily average or will find the max, min ..... later. Here are the code

Private Sub CommandButton1_Click()
Dim sal(1 To 240000, 0 To 23, 2 To 54) As Double
Dim saldaily(1 To 9855, 2 To 54) As Double
Dim s As Double
Dim salmonthly(1 To 12, 2 To 54) As Single
Dim nrow, nhour, ncol, nday, nmonth, k As Integer
nday = 1
nrow = 7
'For nrow = 1 To 8760
' If nrow = 8760 Then
' MsgBox "hi"
' End If
While nrow <= 239067
For nhour = 0 To 23
For ncol = 2 To 54
sal(nday, nhour, ncol) = Sheets("Sheet1").Cells(nrow, ncol).Value
Next ncol
nrow = nrow + 1
If nhour = 23 Then
nday = nday + 1
End If
Next nhour
Wend
For nday = 1 To 9961
For ncol = 2 To 15
s = 0
For nhour = 0 To 23
s = s + sal(nday, nhour, ncol)
Next nhour
saldaily(nday, ncol) = s / 24
Next ncol
Next nday


For nday = 1 To 9961
For ncol = 2 To 15
Sheets("daily").Cells(nday + 1, ncol).Value = saldaily(nday, ncol)
Next ncol
Next nday


End Sub



When running the code it said:
Run time error '7'
Out of memory


Please help to over come this issue.
Thank you
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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