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
 

Some videos you may like

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

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,485
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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,730
Messages
5,488,540
Members
407,643
Latest member
samerf86

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top