Compile Error - User-defined Type Not Defined

aread

New Member
Joined
Dec 27, 2019
Messages
37
Office Version
  1. 365
Platform
  1. Windows
I have been trying to use code I found online and tweaked a little to combine and sum duplicates, but I need to have two values for each key. It then needs to paste that data in a worksheet called "Consolidation" in rows A, B, and C. I am currently getting the error on the line Private Function ReadMultiItems() As Dictionary and am not sure what is wrong. I have added References that others have recommended online as well, but their problems were different. Any suggestions?

Here is some sample data from columns A, B, and M:

Vendor-MatlQty in OPUnAmt.in loc.cur.
30000586-10197957,25712,772.32
30000555-10197959203,072.80
30000571-10197954,8966,462.72
30000586-10197954,8388,514.88
30000586-10197952,4194,257.44
30000571-10197954,8966,462.72
30000572-1019795357335.58
30000586-10197957,25712,772.32
30000589-10196581,3235,702.13
30000601-101965210,00014,730.00
30000593-101955111,0247,496.32
30000593-101955111,0247,496.32
30000600-101947914,968.70240,864.56
20000153-1019425188,607291,831.61
20000153-1019425187,788290,564.37
20000153-1019425188,139291,107.47
20000153-1019425186,274288,221.76

I have a Class Module with the following:
VBA Code:
Public MatlVend As String
Public Volume As Long
Public Spend As Long

And then the module contains the following code:

VBA Code:
Sub Main()

    Dim dict As Dictionary
    
    ' Read the data to the dictionary
    Set dict = ReadMultiItems
    
    ' Write the Dictionary contents to the Immediate Window(Ctrl + G)
    WriteToImmediate dict
    
    ' Write the Dictionary contents to a worksheet
    WriteToWorksheet dict, ThisWorkbook.Worksheets("Consolidation")

End Sub

Private Function ReadMultiItems() As Dictionary

    ' Declare and Create the Dictionary
    Dim dict As New Dictionary
    
    ' Get the worksheet
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Worksheets("Sheet1")
    
    ' Get the range of all the adjacent data using CurrentRegion
    Dim rg As Range
    Set rg = sh.Range("A1").CurrentRegion

    Dim oMatl As clsMatl, i As Long, MatlVend As String
    ' read through the data
    For i = 2 To rg.Rows.Count
        
        MatlVend = rg.Cells(i, 1).Value
        
        ' check if the customerID has been added already
        If dict.Exists(MatlVend) = True Then
            ' Get the existing customer object
            Set oCust = dict(MatlVend)
        Else
            ' Create a new clsCustomer object
            Set oCust = New clsMatl
        
             ' Add the new clsCustomer object to the dictionary
            dict.Add MatlVend, oMatl
        End If
        
        ' Set the values
        oMatl.MatlVend = MatlVend
        oMatl.Volume = oMatl.Volume + rg.Cells(i, 2).Value
        oMatl.Spend = oMatl.Spend + rg.Cells(i, 13).Value
            
    Next i
    
    ' Return the dictionary to the Main sub
    Set ReadMultiItems = dict

End Function


' Write the Dictionary contents  to a worksheet
' https://excelmacromastery.com/
Private Sub WriteToWorksheet(dict As Dictionary, sh As Worksheet)
    
    Dim row As Long
    row = 1
    
    Dim key As Variant, oMatl As clsMaterial
    ' Read through the dictionary
    With ThisWorkbook.Worksheets("Consolidation")
    For Each key In dict.Keys
        Set oMatl = dict(key)
        With oMatl
            ' Write out the values
            sh.Cells(row, 1).Value = .MatlVend
            sh.Cells(row, 2).Value = .Volume
            sh.Cells(row, 3).Value = .Spend
            row = row + 1
        End With
        
    Next key
    End With
End Sub
 
I have an error "Can't perform requested operation"

I tried adding a new one and got the same error.
 
Upvote 0

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)
Where & when did you get that error?
 
Upvote 0
I typed in the new name in the Properties window & hit Enter. That is when the error popped up.
 
Upvote 0
Make sure that you are not trying to run the code.
In the VBE click the square blue "Reset" button & try again.
 
Upvote 0
Ok, I have a Run-time error 91: Object variable or With block variable not set where I am trying to set the values: oMatl.MatlVend = MatlVend
 
Upvote 0
What is your current code, because the code you originally posted shouldn't even get to that point.
 
Upvote 0
Sure. The class module named ClsMatl:

VBA Code:
Public MatlVend As String
Public Volume As Long
Public Spend As Long

And the main code:

VBA Code:
Sub Main()

    Dim dict As Dictionary
    
    ' Read the data to the dictionary
    Set dict = ReadMultiItems
    
    ' Write the Dictionary contents to a worksheet
    WriteToWorksheet dict, ThisWorkbook.Worksheets("Consolidation")

End Sub

Private Function ReadMultiItems() As Dictionary

    ' Declare and Create the Dictionary
    Dim dict As New Dictionary
    
    ' Get the worksheet
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Worksheets("Sheet1")
    
    ' Get the range of all the adjacent data using CurrentRegion
    Dim rg As Range
    Set rg = sh.Range("A1").CurrentRegion

    Dim oMatl As ClsMatl, i As Long
    Dim MatlVend As String
    
    ' read through the data
    For i = 2 To rg.Rows.Count
        
        MatlVend = rg.Cells(i, 1).Value
        
        ' check if the customerID has been added already
        If dict.Exists(MatlVend) = True Then
            ' Get the existing customer object
            Set oCust = dict(MatlVend)
        Else
            ' Create a new clsCustomer object
            Set oCust = New ClsMatl
        
             ' Add the new clsCustomer object to the dictionary
            dict.Add MatlVend, oMatl
        End If
        
        ' Set the values
        oMatl.MatlVend = MatlVend
        oMatl.Volume = oMatl.Volume + rg.Cells(i, 2).Value
        oMatl.Spend = oMatl.Spend + rg.Cells(i, 13).Value
            
    Next i
    
    ' Return the dictionary to the Main sub
    Set ReadMultiItems = dict

End Function


' Write the Dictionary contents  to a worksheet
' https://excelmacromastery.com/
Private Sub WriteToWorksheet(dict As Dictionary, sh As Worksheet)
    
    Dim row As Long
    row = 1
    
    Dim key As Variant, oMatl As ClsMatl
    ' Read through the dictionary
    With ThisWorkbook.Worksheets("Consolidation")
    For Each key In dict.Keys
        Set oMatl = dict(key)
        With oMatl
            ' Write out the values
            sh.Cells(row, 1).Value = .MatlVend
            sh.Cells(row, 2).Value = .Volume
            sh.Cells(row, 3).Value = .Spend
            row = row + 1
        End With
        
    Next key
    End With
End Sub
 
Upvote 0
I'm surprised that it gets to the oMatl.MatlVend = MatlVend line as you have not changed oCust which is undeclared.
Try changing it to oMatl
 
Upvote 0
Sorry, I thought I had gotten them all. I've been looking at this way too long I guess. That fixed it. Thank you very much for helping me through this one.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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