VBA code error: out of memory

jkauhausen

New Member
Joined
Oct 5, 2014
Messages
1
Hi all,

I am a newbie to VBA> Im am trying to write a code that uses an autofilter to help me select and copy groups (Joey, Cub, Scout, Venturer, Rover, Leader) from my main worksheet ("ALL") and paste the data associated with the group onto a separate worksheet (named after the group) in the same workbook.

I have managed to write a code that can work, but Excel is now telling me that it is out of memory as it is performing the sequence.

My question to all VBA experts out there is: How can I reduce and simplify my code so that it uses less memory and goes a bit faster.

Any help would be GREATLY appreciated!!

Thanks in advance :)

Code:
Private Sub CommandButton1_Click()

Dim lastRow As Long
Dim ALL As Worksheet
Dim rnstart As Variant
Dim rndata As Variant

Set ALL = Sheets("ALL")

Application.ScreenUpdating = False
Application.EnableEvents = False
Sheets("ALL").Activate
Sheets("ALL").AutoFilterMode = False
ActiveSheet.Cells(1, 1).Select


With ALL
Set rnstart = .Range("O5")
Set rndata = .Range(.Range("O5"), .Range("O1000").End(xlUp))
End With

Sheets("JOEY's").Activate
ActiveSheet.Range("A5:AP150").ClearContents
Sheets("CUB's").Activate
ActiveSheet.Range("A5:AP150").ClearContents
Sheets("SCOUT's").Activate
ActiveSheet.Range("A5:AP150").ClearContents
Sheets("VENTURER's").Activate
ActiveSheet.Range("A5:AP150").ClearContents
Sheets("ROVER's").Activate
ActiveSheet.Range("A5:AP150").ClearContents
Sheets("LEADER's").Activate
ActiveSheet.Range("A5:AP150").ClearContents

Sheets("ALL").Activate
ActiveSheet.Cells(1, 1).Select
rnstart.AutoFilter Field:=15, Criteria1:="Joey"
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("JOEY's").Select
ActiveSheet.Cells(5, 1).Select
ActiveSheet.Paste

Sheets("ALL").Activate
rnstart.AutoFilter Field:=15, Criteria1:="Cub"
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("CUB's").Select
ActiveSheet.Cells(5, 1).Select
ActiveSheet.Paste

Sheets("ALL").Activate
rnstart.AutoFilter Field:=15, Criteria1:="Scout"
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("SCOUT's").Select
ActiveSheet.Cells(5, 1).Select
ActiveSheet.Paste

Sheets("ALL").Activate
rnstart.AutoFilter Field:=15, Criteria1:="Venturer"
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("VENTURER's").Select
ActiveSheet.Cells(5, 1).Select
ActiveSheet.Paste


Sheets("ALL").Activate
rnstart.AutoFilter Field:=15, Criteria1:="Rover"
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("ROVER's").Select
ActiveSheet.Cells(5, 1).Select
ActiveSheet.Paste

Sheets("ALL").Activate
rnstart.AutoFilter Field:=15, Criteria1:="Leader"
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("LEADER's").Select
ActiveSheet.Cells(5, 1).Select
ActiveSheet.Paste


Sheets("ALL").Activate
Sheets("ALL").AutoFilterMode = False
Application.CutCopyMode = False
ActiveSheet.Cells(1, 1).Select

Application.ScreenUpdate = True
Application.EnableEvents = True


End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Does it help if you avoid all the Selecting?

Code:
Private Sub CommandButton1_Click()
    Dim ALL As Worksheet
    Dim rnstart As Variant
    Set ALL = Sheets("ALL")
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Sheets("JOEY's").Range("A5:AP150").ClearContents
    Sheets("CUB's").Range("A5:AP150").ClearContents
    Sheets("SCOUT's").Range("A5:AP150").ClearContents
    Sheets("VENTURER's").Range("A5:AP150").ClearContents
    Sheets("ROVER's").Range("A5:AP150").ClearContents
    Sheets("LEADER's").Range("A5:AP150").ClearContents
    With ALL
        .AutoFilterMode = False
        Set rnstart = .Range("O5")
        rnstart.AutoFilter Field:=15, Criteria1:="Joey"
        .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Sheets("JOEY's").Cells(5, 1)
        rnstart.AutoFilter Field:=15, Criteria1:="Cub"
        .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Sheets("CUB's").Cells(5, 1)
        rnstart.AutoFilter Field:=15, Criteria1:="Scout"
        .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Sheets("SCOUT's").Cells(5, 1)
        rnstart.AutoFilter Field:=15, Criteria1:="Venturer"
        .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Sheets("VENTURER's").Cells(5, 1)
        rnstart.AutoFilter Field:=15, Criteria1:="Rover"
        .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Sheets("ROVER's").Cells(5, 1)
        rnstart.AutoFilter Field:=15, Criteria1:="Leader"
        .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Sheets("LEADER's").Cells(5, 1)
        .AutoFilterMode = False
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,917
Members
449,055
Latest member
KB13

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