Automation Error when building collection

bbrother

New Member
Joined
Nov 23, 2015
Messages
41
Dear Excel Experts,

I have a simple piece of VBA code with some (messy) error handling that builds a collection by constructing a string and seeing if it exists in a larger collection.

This code generates an application execution error on the second pass through the for next loop.

Code:
Private Sub UserForm_Initialize()
    Dim i As Long
    Dim Product As String
    Dim PCores As Collection
    
    If MyTools.PLProducts.count = 0 Then Call ConfiguratorMain.DataLoader
    
Resum:
    ConfidentialL.Caption = ConfiguratorMain.ConfidentialL.Caption
    OutputLB.ColumnCount = 4
    OutputLB.TextAlign = fmTextAlignRight
    
    Set PCores = New Collection
    On Error GoTo eh  'If the constructed product name doesn't exist in the collection, then skip
    For i = 1 To 2 ^ 16
        Product = "5TB-XFD-" & i
        If MyTools.PLProducts(Product) Then
            PCores.Add i
        End If
eh:
    Next
    On Error GoTo 0


End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

bbrother

New Member
Joined
Nov 23, 2015
Messages
41
Maybe....
Code:
PCores.Add Product
HTH, Dave

Nope, it's really more convenient for me to have a number I can calculate with. That integer represents number of CPU cores for which I need to use for various calculations. Are you saying that using a collection of strings rather than a collection of Longs will avoid the error?
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,791
No, I was just guessing that you were trying to make a new collection of products from the MyTools.PLProducts collection. That doesn't really explain the error. Maybe...
Code:
Product = "5TB-XFD-" & CSTR(I)
Why 2 to the power of 16? You could take the error code out and insert some msgboxes to find out where and what is actually causing the error. HTH. Dave
 

Forum statistics

Threads
1,136,792
Messages
5,677,761
Members
419,718
Latest member
ALWP

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
Top