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
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Do you have "Microsoft Scripting Runtime" Checked in VBE>Tools>References?
 
Upvote 0
Have you set a reference to the "Microsoft Scripting Runtime" library?
 
Upvote 0
No, that one was missing...

Now I am getting the same error and it selects the text "oMatl As clsMatl" but when I click Ok, it still highlights Private Function ReadMultiItems() As Dictionary
 
Upvote 0
Did you copy the class code that would have come with the code you posted?
If so did you follow the instructions on how to use it?
 
Upvote 0
There weren't any instructions other than copy the code and paste it into a class module, which I did.
 
Upvote 0
Did you rename the class module to clsMatl
 
Upvote 0
As a matter of interest, where did you get the code from?
 
Upvote 0
Whilst it doesn't say it the class module needs to be renamed, in the code you linked to it should be ClsCustomer, but as you have changed it, it needs to be clsMatl
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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