VBA Runtime Error 7: Out of Memory?

Peter h

Active Member
Joined
Dec 8, 2015
Messages
417
Hey guys, I've got a little project I'm working on. More of a personal project than anything. I making a workbook that I can build, store, and analyze magic the gathering deck lists. I have a userform that allows me to create a new sheet based on a named whatever name I give it, and then add card data to a list in that new sheet. In order to analyze the decks, I have certain ranges on each sheet that have formulas of the stats I want to track. I wasn't really sure how to do this in a short compact manor, so my code is quite long and cumbersome. Now, when I try to have the code create charts I'm getting the runtime error 7: Out of Memory. Would you guys mind looking over my code, and help me make it less taxing on my system? It breaks on the Red Highlighted line. Here's the code:

Code:
Private Sub cb_Create_Click()Dim lRow As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim cht As Chart
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1")
lRow = ws1.Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False


If tb_Name = "" Then
    MsgBox "Name your deck."
    GoTo CleanUp
End If
For Each ws In ThisWorkbook.Sheets
    If ws.Name = tb_Name.Text Then
        MsgBox "That name is already used. Try something else."
        GoTo CleanUp
    End If
Next
 
Sheets.Add(After:=ws1).Name = tb_Name.Text
Sheet1.Activate
ws1.Cells(lRow, 1) = tb_Name.Text
cb_Add.Enabled = True
cb_Create.Enabled = False


Set ws = wb.Sheets(tb_Name.Text)
    With ws
        .Range("A1") = "Main Deck"
        .Range("E1") = "Sideboard"
        .Range("A2") = "Card Name"
        .Range("B2") = "Card Type"
        .Range("C2") = "Casting Cost"
        .Range("D2") = "Converted Mana Cost"
        .Range("E2") = "Card Name"
        .Range("F2") = "Card Type"
        .Range("G2") = "Casting Cost"
        .Range("H2") = "Converted Mana Cost"
        .Range("I2") = "0"
        .Range("J2") = "1"
        .Range("K2") = "2"
        .Range("L2") = "3"
        .Range("M2") = "4"
        .Range("N2") = "5"
        .Range("O2") = "6"
        .Range("P2") = "7+"
        .Range("Q2") = "Average"
        .Range("I4") = "W"
        .Range("J4") = "U"
        .Range("K4") = "B"
        .Range("L4") = "R"
        .Range("M4") = "G"
        .Range("N4") = "CL"
        .Range("I6") = "Creature"
        .Range("J6") = "Artifact"
        .Range("K6") = "Enchantment"
        .Range("L6") = "Planeswalker"
        .Range("M6") = "Instant"
        .Range("N6") = "Sorcery"
        .Range("O6") = "Land"
        .Range("I3").Formula = "=COUNTIF($D$3:$D$1000, 0)"
        .Range("J3").Formula = "=COUNTIF($D$3:$D$1000, 1)"
        .Range("K3").Formula = "=COUNTIF($D$3:$D$1000, 2)"
        .Range("L3").Formula = "=COUNTIF($D$3:$D$1000, 3)"
        .Range("M3").Formula = "=COUNTIF($D$3:$D$1000, 4)"
        .Range("N3").Formula = "=COUNTIF($D$3:$D$1000, 5)"
        .Range("O3").Formula = "=COUNTIF($D$3:$D$1000, 6)"
        .Range("P3").Formula = "=COUNTIF($D$3:$D$1000, "">= 7"")"
        .Range("Q3").Formula = "=AVERAGE($D:$D)"
        .Range("I5").Formula = "=COUNTIF($C3:$C1000, ""*W*"")"
        .Range("J5").Formula = "=COUNTIF($C3:$C1000, ""*U*"")"
        .Range("K5").Formula = "=COUNTIF($C3:$C1000, ""*B*"")"
        .Range("L5").Formula = "=COUNTIF($C3:$C1000, ""*R*"")"
        .Range("M5").Formula = "=COUNTIF($C3:$C1000, ""*G*"")"
        .Range("N5").Formula = "=COUNTIF($C3:$C1000, "">= 0"")"
        .Range("I7").Formula = "=COUNTIF($B$3:$B$1000, ""*Creature*"")"
        .Range("J7").Formula = "=COUNTIF($B$3:$B$1000, ""*Artifact*"")"
        .Range("K7").Formula = "=COUNTIF($B$3:$B$1000, ""*Enchantment*"")"
        .Range("L7").Formula = "=COUNTIF($B$3:$B$1000, ""*Planeswalker*"")"
        .Range("M7").Formula = "=COUNTIF($B$3:$B$1000, ""*Instant*"")"
        .Range("N7").Formula = "=COUNTIF($B$3:$B$1000, ""*Sorcery*"")"
        .Range("O7").Formula = "=COUNTIF($B$3:$B$1000, ""*Land*"")"
        .Cells.EntireColumn.AutoFit
        
    End With


Set cht = ws.Shapes.AddChart.Chart
With cht
[COLOR=#ff0000]    .Name = "CMC"[/COLOR]
    .ApplyChartTemplate ( _
        "C:\Users\ESTHERRAY\AppData\Roaming\Microsoft\Templates\Charts\CMC.crtx")
    .SetSourceData Source:=ws.Range("I2:P3")
    .ChartTitle.Text = "Converted Mana Cost"
End With
CleanUp:
    Call pop_cb
    ThisWorkbook.Save


    On Error Resume Next
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
Exit Sub
End Sub

Thank You!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
After a little more googling I found the solution. I should've put:

Code:
.Parent.Name = "CMC"

I'm going to have to research and see exactly what the "Parent" function does, but I came across that and it solved my problem.
 
Upvote 0

Forum statistics

Threads
1,215,180
Messages
6,123,502
Members
449,100
Latest member
sktz

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