Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"User-defined type not defined" for simple custom type

In Excel 2007 I have the following very simple code in VBA:

Public Type specType
    sb As Long
End Type


Private Sub MyButton_Click()
    Dim spec As specType

    '...
End Sub

When the button is clicked, i get a "User-defined type not defined" Error on the "Dim spec As specType" line... why? Do I have to move my user defined types to a special location?

like image 458
Florian Ledermann Avatar asked Jan 13 '11 12:01

Florian Ledermann


People also ask

How do you define a user-defined Type in VBA?

The Type statement can be used only at the module level. After you have declared a user-defined type by using the Type statement, you can declare a variable of that type anywhere within the scope of the declaration. Use Dim, Private, Public, ReDim, or Static to declare a variable of a user-defined type.

What is object required in VBA?

Object Required in Excel VBA. Object required is an error which is caused at run time when we have defined any variable which is not an object but we try to assign some values using a SET statement. This error is a run time error that arises for various reasons.


1 Answers

Turns out Types have to be defined before any functions in the module, otherwise they simply won't be recognized (without giving you an error).

like image 189
Florian Ledermann Avatar answered Sep 24 '22 12:09

Florian Ledermann