Force use of standard function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have a class module that has a function called Year(). Normally, this is
fine but in the class module I use the standard vba function Year() to
extract the year from a date. However, vba thinks that this is the class
modules Year() function. How can I tell it to use the standard Year()
function without changing the name of the Year() function in my class module.
 
Seriously Mark, rename your function. I am not saying there isn't a way
round this but its a bad idea to have a procedure of yours with the same name
as a standard VB function.
 
Mark,

You can't have it both ways, so RENAME your function:

Sub Test()
MsgBox myYear("1/2/2007")
End Sub

Function myYear(inDate As Date) As Integer
myYear = Year(inDate)
End Function

HTH,
Bernie
MS Excel MVP
 
Best not to name functions same as VBA's functions. However this appears to
work -

' normal module
Sub test()
Dim c As New Class1

MsgBox c.Year(2&) & vbCr & _
Year(4&) & vbCr & _
VBA.DateTime.Year(Date)

End Sub

Function Year(n As Long) As Long
Year = n + 2000
End Function

'code in Class1
Public Function Year(n As Long) As Long
Year = n + 2000
End Function

Regards,
Peter T
 
You shouldn't use VBA reserved words as function names. However,
you can force VBA to use its own function by including the VBA
library name:

Dim Res As Integer
Res = VBA.Year(whatever)

When you are calling your own Year function from within the class
module, use

Res = Me.Year(whatever)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



message
news:[email protected]...
 
You can't have it both ways, so RENAME your function:

Actually you can have it both ways. Use

VBA.Year for the VBA function and
Me.Year for the class module function.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top