Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to use user defined type 1

Status
Not open for further replies.

venkman

Programmer
Oct 9, 2001
467
US
I defined a type with the statement:

Type blah
i As Integer
End Type

Then tried to use the new type in the same module:

Public Sub asub()
Dim myblah As blah
myblah.i = 5
End Sub


And got an undefined type compile error for the line:
dim myblah as blah

Why?
-Venkman
 
I have no idea. This should work just fine (and does for me). What happens if you move the type definition into your sub? Maybe we'll spot something funny if you post more of your code.

Rob
[flowerface]
 
Never mind my question about moving it into the sub - type definitions are always at module level. Still puzzled.
Rob
[flowerface]
 
well my thought was that it might have to do with some problem with settings under "references...", but that looks fine to me.
Let me know if you think of anything.
-Venkman
 

I checked it out with word and excel and it worked so long as I added a module and declared it as such...

[tt]
Public Type MyType
Test As Boolean
End Type
[/tt]

Good Luck
 
Venkman,
Since two of us have successfully done what you're having problems with, chances are there is something in your code that messing up. If you distill your code down to JUST the sections in your first post, do you still get the error?
Rob
[flowerface]
 
Rob,
I'll try that, I'm guessing the problem is the opposite of what you said. Given that 2 of you have it working, it's not the code that's the problem. I would think this would indicate that some setting is screwed up or the worksheet I'm working with is somehow corrupted so that certain things in vb are not working (which I've seen before). I'll see if a blank sheet with no other vb code than the one posted works and get back to you guys if I'm able to locate the problem. In the meantime, I appreciate the sanity check that both of you guys have provided.
Thanks,
-Venkman
 
Okay, figured it out... type definitions must come before all sub/function definitions in a module. The problem before was that in my file there was subroutines before that particular type definition. If the two ppl who responded to my post (Rob and vb5prgrmr) have some extra time it would be nice if you guys could confirm this. <Insert standard Microsoft bashing for not mentioning this caveat in their help file here>
-Venkman
 
I tried it, and ran &quot;debug-compile project&quot;. VBA yelled at me on the type definition, telling me that only comments may appear after an end sub. So you're right - they have to be right up top. The same holds also for regular dim statements, by the way - it's not a peculiarity of type definitions.

Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top