custom function does not work in Sub code

dennisli

Well-known Member
Joined
Feb 20, 2004
Messages
1,070
Good morning,
I have a custom function TreatYear. It works if I use it in Excel worksheets.
But when I put in Sub code, the builtin function only give me “No Treaty Year” results. The Sub code works well with other With End With.
Any ideas will be appreciated.
Dennis

Here is my Sub code:
With Range("F9:F" & DataLastRow2)
.Formula = "=TreatYear(Data!E9)"
.Value = .Value
End With


Here is my simple TreatYear function:

Public Function TreatYear(LossDate)

Const TY1985B = #10/1/1985#
Const TY1986B = #12/1/1986#
Const TY1987B = #12/1/1987#

Const TY1985E = #11/30/1986#
Const TY1986E = #11/30/1987#
Const TY1987E = #11/30/1988#

Select Case LossDate
Case TY1985B To TY1985E: TreatYear = 1985
Case TY1986B To TY1986E: TreatYear = 1986
Case TY1987B To TY1987E: TreatYear = 1987
Case Else: TreatYear = "No Treaty Year"
End Select

End Function
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
Works fine for me.:)

Do you actually have real date values in column E?
 

Forum statistics

Threads
1,136,701
Messages
5,677,284
Members
419,684
Latest member
BOB101

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
Top