View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Bill Renaud Bill Renaud is offline
external usenet poster
 
Posts: 417
Default Macro or other code for deteming Months, Years, Quarter based on d

Here is a suggestion to help you get started writing UDFs (user-defined
functions). They really aren't hard at all, once you figure out some
basics. Start up the Visual Basic editor (Alt+F11 or use the menu), then
Insert|Module (standard code module, not a Class Module). Paste the
following code into the editor window, then Debug|Compile VBA Project.
Close the VBA editor window. You can now use your UDF just like any other
built-in Excel function. After typing the "=" sign in the formula bar,
select "User Defined" at the bottom of the Function category list box, then
select your UDF in the Function name list box. Fill in the arguments as
usual.

You won't need to declare the types of the arguments. They will be variants
by default. Also, the return value of the function will be variant by
default, since it is not declared after the closing right paren after the
arguments.

To test to see if an input argument is an empty cell, use the IsEmpty
function. Notice inside each If statement that I put an Exit Function
statement after the calculation for that case. This will prevent your code
from recalculating the function again (wrongly) in the lines of code that
follow below. Just make sure that you do the calculation before any Exit
Function statement. Be sure to cover all cases, or return an error value
(see several paragraphs below).

You will probably have 3 functions (one each for "AccountMonths",
"AccountYears", and "AccountQuarters") when you get done. You might be able
to factor out a lot of the common logic for each of these. I used
"AccountMonths" as the name of the example function below so as not to
confuse it with Months, which might be a worksheet function or something.

NOTE: You will have to define what your function is, as it is not really
clear to others in the newsgroup from your description. I simply subtracted
the 2 values that you indicated in your text, but I know this will have to
be divided by 12, 4, dates rounded to ends of months, etc. or something
else. Use the following statement format example to call worksheet
functions from inside your UDF:

YourVariable = Application.WorksheetFunction.Max(arg1,arg2)

If you want to return an error value, then use the following statement
(there are several values for the CVErr argument that you can use):

AccountMonths=CVErr(xlErrValue)

Post additional info if you need more help.

'----------------------------------------------------------------------
Public Function AccountMonths(StartDate, AdjStartDate, _
CurrentDate, TermDate)

Dim dtToday As Date

'Get current system date once, for consistent processing.
dtToday = Date

'If both StartDate and AdjStartDate are populated (and are the same)
'and no other date is populated, then calculate the months, years
'and quarter based on StartDate and today's date.
If Not IsEmpty(StartDate) _
And Not IsEmpty(AdjStartDate) _
And StartDate = AdjStartDate _
And IsEmpty(CurrentDate) _
And IsEmpty(TermDate) _
Then
'Change as needed.
AccountMonths = dtToday - StartDate
Exit Function
End If

'Calculate months, years and quarter based on the TermDate
'and the StartDate, if no current date is populated.
If Not IsEmpty(AdjStartDate) _
And IsEmpty(CurrentDate) _
And Not IsEmpty(TermDate) _
Then
'Change as needed.
AccountMonths = TermDate - StartDate
Exit Function
End If

'If CurrentDate is populated, then calculate the time
'between the AdjStartDate and CurrentDate.
If Not IsEmpty(CurrentDate) _
Then
'Change as needed.
AccountMonths = CurrentDate - AdjStartDate
Exit Function
End If

'More cases below as needed.
'To return an error, do the following:
'See the different values of arguments in the
'XlCVError class in the Object Browser.
AccountMonths = CVErr(xlErrValue)

End Function

--
Regards,
Bill Renaud