r/visualbasic • • Jul 25 '17

VB.NET error "Insufficient memory to continue the execution of the program"

Hi, I'm running a listbox that is pulling data from an excel worksheet. This works by copying each line item to another sheet (that code works) and then pulling that value into the listbox. I get the insufficient memory error on the following line:

lbxResults.Items.AddRange(GlobalVariables.oWb.Worksheets("sheet2").Range("B1:B" & lastrow, "A1:U21").Value)

This is basically some modified vba code, so that syntax may bot be correct for .NET. Just looking how to proceed to get this working.

Thanks in advance!

6 Upvotes

13 comments sorted by

1

u/EveryoneLikesMe VB.Net Advanced Jul 25 '17

Can you post the entire code of the relevant method?

1

u/ImpeachJohnV Jul 25 '17
 With GlobalVariables.oWb.Worksheets("Mastersheet")
                 GlobalVariables.oWb.Worksheets("Mastersheet").select
                 rng = GlobalVariables.oExcel.Range("D2:D750")
                 crange = rng.Find(What:=txtSearch.Text, LookIn:=xlvaluable, LookAt:=counter)
                 firstaddress = crange.Address
                 rowcount = xlWs.UsedRange.Rows.Count
                 If crange Is Nothing Then
                    MsgBox("Search not found.", vbOKOnly)
                    Exit Sub
                 End If
                 Do
                    crange.EntireRow.Copy(GlobalVariables.oWb.Worksheets("sheet2").Range("A" & rowcount).Offset(1))
                     lbxResults.MultiColumn = 24

                    lastrow = GlobalVariables.oWb.Worksheets("sheet2").Cells(GlobalVariables.oWb.Worksheets("Sheet2").Rows.Count, 1).Row
                     lbxResults.Items.AddRange(GlobalVariables.oWb.Worksheets("sheet2").Range("B1:B" & lastrow, "A1:U21").Value)
                    crange = rng.FindNext(crange)
                 Loop While crange.Address <> firstaddress

1

u/chrwei Jul 25 '17

this issue might resolve around Range("B1:B" & lastrow, "A1:U21")

you're specifying 2 ranges using syntax that is designed for 2 cells, and specifically 2 cell objects, not string. A1 is a cell, A1:U21 is a range. I'm not really sure what you expect this to do.

1

u/ImpeachJohnV Jul 25 '17

frankly, I don't get why it specifies 2 ranges either. This was coded by a previous employee - it runs in vba but I'm also thrown for a loop regarding the 2 ranges.

1

u/chrwei Jul 25 '17

it's possible that VBA is ignoring the 2nd one and the .net layer is confused.

1

u/ImpeachJohnV Jul 25 '17

I tried shortening it to just a single range and this is the full resulting error message

An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in Microsoft.VisualBasic.dll

Additional information: Overload resolution failed because no Public 'AddRange' can be called with these arguments:

'Public Sub AddRange(value As System.Windows.Forms.ListBox.ObjectCollection)':

    Argument matching parameter 'value' cannot convert from 'Object(,)' to 'ObjectCollection'.

'Public Sub AddRange(items As System.Object())':

    Argument matching parameter 'items' cannot convert from 'Object(,)' to 'Object()'.

I assume that those subs are from .net

1

u/chrwei Jul 25 '17

what's the exact code? Object(,) is weird. and maybe you don't need the .Value?

1

u/ImpeachJohnV Jul 25 '17
With GlobalVariables.oWb.Worksheets("Mastersheet")
                 GlobalVariables.oWb.Worksheets("Mastersheet").select
                  rng = GlobalVariables.oExcel.Range("D2:D750")
                  crange = rng.Find(What:=txtSearch.Text, LookIn:=xlvaluable, LookAt:=counter)
                  firstaddress = crange.Address
                 rowcount = xlWs.UsedRange.Rows.Count
                  If crange Is Nothing Then
                     MsgBox("Search not found.", vbOKOnly)
                     Exit Sub
                  End If
                  Do
                    crange.EntireRow.Copy(GlobalVariables.oWb.Worksheets("sheet2").Range("A" & rowcount).Offset(1))
                      lbxResults.MultiColumn = 24

                     lastrow = GlobalVariables.oWb.Worksheets("sheet2").Cells(GlobalVariables.oWb.Worksheets("Sheet2").Rows.Count, 1).Row
                      lbxResults.Items.AddRange(GlobalVariables.oWb.Worksheets("sheet2").Range("B1:B" & lastrow, "A1:U21").Value)
                    crange = rng.FindNext(crange)
                 Loop While crange.Address <> firstaddress

1

u/chrwei Jul 25 '17

that still has Range("B1:B" & lastrow, "A1:U21")

1

u/ImpeachJohnV Jul 25 '17

Yes, sorry, I adjusted it in the code; I had linked the code earlier in another reply and just copy pasted it from there - my mistake.

1

u/ImpeachJohnV Jul 25 '17

Cutting .value yields

An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in Microsoft.VisualBasic.dll

Additional information: Overload resolution failed because no Public 'AddRange' can be called with these arguments:

'Public Sub AddRange(value As System.Windows.Forms.ListBox.ObjectCollection)':

    Argument matching parameter 'value' cannot convert from '__ComObject' to 'ObjectCollection'.

'Public Sub AddRange(items As System.Object())':

    Argument matching parameter 'items' cannot convert from '__ComObject' to 'Object()'.

1

u/chrwei Jul 25 '17

seems like you need to convert the COM object range to a generic object.

I've only done this stuff in vba and vb6, not sure if there's a better way for .net.

1

u/ImpeachJohnV Jul 25 '17

I don't assume there's a CObj() method lol