VBA "out of memory" help

VBAhelp33

New Member
Joined
Sep 13, 2020
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I have been building some code and I am now getting this "out of memory" error. Basically I have built a userform which has list1 (lstSelector) which the user can select & remove items to listbox2 and then the user clicks go and the selected data from listbox2 is pasted into an excel worksheet.

It appears to happen to I tell my userform to paste the data in listbox2 to an excel worksheet. (Private Sub CommandButton4_Click())

Googling the error and it appears vague. It says to set ranges & objects to nothing. So I have set my worksheets to nothing but it hasn't stopped my error.

Does anything in my below code look like it cold be causing the error?



Private Sub UserForm_Initialize()

Dim sh As Worksheet
Set sh = Worksheets("Data")

Dim i As Long

For i = 3 To sh.Range("A500").End(xlUp).Row

Me.lstSelector.AddItem sh.Cells(i, 1)

Me.lstSelector.List(lstSelector.ListCount - 1, 1) = sh.Cells(i, 2)

Next i

Set sh = Nothing

End Sub

Private Sub cmdAdd_Click() 'Code to add items from listbox1 to listbox2

Dim iCtr As Long

For iCtr = 0 To Me.lstSelector.ListCount - 1

If Me.lstSelector.Selected(iCtr) = True Then

Me.ListBox2.AddItem Me.lstSelector.List(iCtr)

Me.ListBox2.List(ListBox2.ListCount - 1, 1) = Me.lstSelector.List(iCtr, 1)

End If

Next iCtr

For iCtr = Me.lstSelector.ListCount - 1 To 0 Step -1

If Me.lstSelector.Selected(iCtr) = True Then

Me.lstSelector.RemoveItem iCtr

End If

Next iCtr


End Sub


Private Sub CommandButton2_Click() 'Remove selected items from listbox2 and move them back to listbox1

Dim iCtr As Long

For iCtr = 0 To Me.ListBox2.ListCount - 1

If Me.ListBox2.Selected(iCtr) = True Then

Me.lstSelector.AddItem Me.ListBox2.List(iCtr)

Me.lstSelector.List(lstSelector.ListCount - 1, 1) = Me.ListBox2.List(iCtr, 1)

End If

Next iCtr


For iCtr = Me.ListBox2.ListCount - 1 To 0 Step -1

If Me.ListBox2.Selected(iCtr) = True Then

Me.ListBox2.RemoveItem iCtr

End If

Next iCtr

End Sub



Private Sub CommandButton3_Click() 'Remove all items in listbox2 and return them to listbox1

Dim iCtr As Long

For iCtr = 0 To Me.ListBox2.ListCount - 1

Me.lstSelector.AddItem Me.ListBox2.List(iCtr)

Me.lstSelector.List(lstSelector.ListCount - 1, 1) = Me.ListBox2.List(iCtr, 1)

Next iCtr

Me.ListBox2.Clear

End Sub



Private Sub CommandButton4_Click() 'paste selected items in listbox2 to excel worksheet

Dim i As Long

For i = 0 To Me.ListBox2.ListCount - 1

ListBox2.Selected(i) = True

Next

Dim PS As Worksheet

Set PS = Worksheets("PortfolioSummary")

For i = 0 To Me.ListBox2.ListCount - 1

If Me.ListBox2.Selected(i) = True And Me.ListBox2.List(i, 1) <> "" Then

PS.Range("E52").End(xlUp).Offset(1, 0) = Me.ListBox2.List(i, 0)

PS.Range("E52").End(xlUp).Offset(0, 1) = Me.ListBox2.List(i, 1)

Else

Me.ListBox2.Selected(i) = False

End If

Next i

Set PS = Nothing

Unload Me

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
VBA Code:
Private Sub UserForm_Initialize()

Dim sh As Worksheet
Set sh = Worksheets("Data")

Dim i As Long

For i = 3 To sh.Range("A500").End(xlUp).Row

Me.lstSelector.AddItem sh.Cells(i, 1)

Me.lstSelector.List(lstSelector.ListCount - 1, 1) = sh.Cells(i, 2)

Next i

Set sh = Nothing

End Sub

Private Sub cmdAdd_Click() 'Code to add items from listbox1 to listbox2

Dim iCtr As Long

For iCtr = 0 To Me.lstSelector.ListCount - 1

If Me.lstSelector.Selected(iCtr) = True Then

Me.ListBox2.AddItem Me.lstSelector.List(iCtr)

Me.ListBox2.List(ListBox2.ListCount - 1, 1) = Me.lstSelector.List(iCtr, 1)

End If

Next iCtr

For iCtr = Me.lstSelector.ListCount - 1 To 0 Step -1

If Me.lstSelector.Selected(iCtr) = True Then

Me.lstSelector.RemoveItem iCtr

End If

Next iCtr


End Sub


Private Sub CommandButton2_Click() 'Remove selected items from listbox2 and move them back to listbox1

Dim iCtr As Long

For iCtr = 0 To Me.ListBox2.ListCount - 1

If Me.ListBox2.Selected(iCtr) = True Then

Me.lstSelector.AddItem Me.ListBox2.List(iCtr)

Me.lstSelector.List(lstSelector.ListCount - 1, 1) = Me.ListBox2.List(iCtr, 1)

End If

Next iCtr


For iCtr = Me.ListBox2.ListCount - 1 To 0 Step -1

If Me.ListBox2.Selected(iCtr) = True Then

Me.ListBox2.RemoveItem iCtr

End If

Next iCtr

End Sub



Private Sub CommandButton3_Click() 'Remove all items in listbox2 and return them to listbox1

Dim iCtr As Long

For iCtr = 0 To Me.ListBox2.ListCount - 1

Me.lstSelector.AddItem Me.ListBox2.List(iCtr)

Me.lstSelector.List(lstSelector.ListCount - 1, 1) = Me.ListBox2.List(iCtr, 1)

Next iCtr

Me.ListBox2.Clear

End Sub



Private Sub CommandButton4_Click() 'paste selected items in listbox2 to excel worksheet

Dim i As Long

For i = 0 To Me.ListBox2.ListCount - 1

ListBox2.Selected(i) = True

Next

Dim PS As Worksheet

Set PS = Worksheets("Summary")

For i = 0 To Me.ListBox2.ListCount - 1

If Me.ListBox2.Selected(i) = True And Me.ListBox2.List(i, 1) <> "" Then

PS.Range("E52").End(xlUp).Offset(1, 0) = Me.ListBox2.List(i, 0)

PS.Range("E52").End(xlUp).Offset(0, 1) = Me.ListBox2.List(i, 1)

Else

Me.ListBox2.Selected(i) = False

End If

Next i

Set PS = Nothing

Unload Me

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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