Avatar of mcs26
mcs26
 asked on

VBA User Definded Types

Hi,

I'm little confused as to why the function below does not work. The error message is, Compile Error: User-defined type is not defined. What's strange is if a turn the function into a sub instead and take out the last line, BenchmarkDates = BMDates
the code works and recognises the user defined type.

Use user defined types a lot in the same module and have no issues?

Thanks for any help,
M

Private Type TypeBMDates
    YTD As Date
    MTD As Date
    TMinus5 As Date
    TMinus1 As Date
End Type

Function BenchmarkDates() As TypeBMDates

Dim BMDates As TypeBMDates

    BMDates.YTD = DateSerial(Year(Now), 1, 0)
    BMDates.YTD = Last_Working_Day(BMDates.YTD)
    
    BMDates.MTD = DateSerial(Year(Now), Month(Now), 0)
    BMDates.MTD = Last_Working_Day(BMDates.MTD)
    
    BMDates.TMinus5 = Last_Working_Day(DateAdd("d", -7, Date))
    
    BMDates.TMinus1 = Last_Working_Day(DateAdd("d", -1, Date))

    BenchmarkDates = BMDates
    
    End Function

Open in new window

Visual Basic ClassicMicrosoft ApplicationsProgramming Languages-Other

Avatar of undefined
Last Comment
aikimark

8/22/2022 - Mon
GrahamSkan

The Type should be declared as Public
GrahamSkan

I think that there might be a bug (or at least a difference between the documentation and what actually happens)
ASKER CERTIFIED SOLUTION
vb_elmar

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
aikimark

What is Last_Working_Day()?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck