Formula help/suggestions?

G

Gadgetman

I'm looking for help some help with a formula.

How would I go about a formula for automatically adding 8 hours to a
number each calendar month? I would like to open it and have it done for
me if that's possible rather than having to remember to add it each
month or forgetting if I did or not.


For instance, i start out with the # 248 in a cell that I would like to
automatically add 8 to each calendar month.
 
C

Clif McIrvin

Gadgetman said:
I'm looking for help some help with a formula.

How would I go about a formula for automatically adding 8 hours to a
number each calendar month? I would like to open it and have it done
for
me if that's possible rather than having to remember to add it each
month or forgetting if I did or not.


For instance, i start out with the # 248 in a cell that I would like
to
automatically add 8 to each calendar month.


My solution requires a user defined function - so you need to put code
into a code module. You could then use helper cells, or put everything
into the formula. Your question seems to suggest actually changing the
value -- but I'm going to recommend calculating a number to add based on
today's date, so you're going to need to tell the formula what the
beginning date is. For example, to use only one cell that looks like it
changes automagically, use this formula:

= 248 + ( DateDiff ( "Jan 1, 2010" ) * 8 )

You could also use three helper cells, and have something like this:

= A1 + ( DateDiff ( B1 ) * C1 )

The UDF:

Option Explicit

Public Function MonthDiff( _
Optional ByVal BeginDate As Variant) As Variant

' Return number of months between BeginDate and today
' (Ignores day of month - calculates 1st to 1st)
' BeginDate in future month returns negative number
' BeginDate missing or not a date returns zero

MonthDiff = 0
Select Case VarType(BeginDate)
Case vbDate
MonthDiff = DateDiff("m", BeginDate, Date)
Case vbString
If IsDate(BeginDate) Then
MonthDiff = DateDiff("m", CDate(BeginDate), Date)
End If
End Select

End Function


If you're not familiar with creating UDFs, do this:
right-click on the sheet tab
click on View Code (the VBEditor window will open)
in the code window that opens up copy and paste everything beginning
with Option Explicit including End Function


Because it is user function code, it is fairly easy to install and use:

1. right-click the tab name near the bottom of the Excel window
2. select View Code - this brings up a VBE window
3. create a code module for your function:
on the Menu Bar, click Insert | Module
4. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

When you save the workbook, the user function will be saved with it.


To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

(instructions and links borrowed from a post by James Ravenswood)
 
S

Stan Brown

I'm looking for help some help with a formula.

How would I go about a formula for automatically adding 8 hours to a
number each calendar month? I would like to open it and have it done for
me if that's possible rather than having to remember to add it each
month or forgetting if I did or not.


For instance, i start out with the # 248 in a cell that I would like to
automatically add 8 to each calendar month.

Is this a homework problem, by any chance?

Here's a hint: "add 8 every month" is the same as "multiply 8 by the
number of months between the base month and today, and add that".
The date functions can easily compute the number of months.

I saw someone suggesting a user-define function. While that's a
possible approach, I don't see any need for it. A worksheet formula
will do what you are asking.
 
C

Clif McIrvin

DATEDIF can give you the number of complete calendar months between
two dates. The syntax is:

=DATEDIF(Date1, Date2, Interval)

where Interval would be "m".

Chip Pearson has more examples of this undocumented function here:

http://www.cpearson.com/excel/datedif.aspx

Hope this helps.

Pete


----------

Undocumented --- that would explain it! I thought I could remember date
calculations being discussed in this room <g>.

Thanks!
 
C

Charabeuh

Hello,

Perhaps something like this:

FirstDate is the name of a cell that contains a date.
This date should match the month and year when your data begin (i.e. your
number is equal to 248).
Then try this formula:

=248+8*(MONTH(TODAY())-MONTH(FirstDate)+12*(YEAR(TODAY())-YEAR(FirstDate)))


________________________________________________________________________________________
"Gadgetman" a écrit dans le message de groupe de discussion :
(e-mail address removed)...

I'm looking for help some help with a formula.

How would I go about a formula for automatically adding 8 hours to a
number each calendar month? I would like to open it and have it done for
me if that's possible rather than having to remember to add it each
month or forgetting if I did or not.


For instance, i start out with the # 248 in a cell that I would like to
automatically add 8 to each calendar month.
 
C

Charabeuh

Sorry,
I did not notice the number could be hours.
If you need hours format:
replace my previous formula with:
="248:00"+"8:00"*(MONTH(TODAY())-MONTH(FirstDate)+12*(YEAR(TODAY())-YEAR(FirstDate)))

and format the cell with format: [h]:mm

______________________________________________________________________________
"Charabeuh" a écrit dans le message de groupe de discussion :
[email protected]...

Hello,

Perhaps something like this:

FirstDate is the name of a cell that contains a date.
This date should match the month and year when your data begin (i.e. your
number is equal to 248).
Then try this formula:

=248+8*(MONTH(TODAY())-MONTH(FirstDate)+12*(YEAR(TODAY())-YEAR(FirstDate)))


________________________________________________________________________________________
"Gadgetman" a écrit dans le message de groupe de discussion :
(e-mail address removed)...

I'm looking for help some help with a formula.

How would I go about a formula for automatically adding 8 hours to a
number each calendar month? I would like to open it and have it done for
me if that's possible rather than having to remember to add it each
month or forgetting if I did or not.


For instance, i start out with the # 248 in a cell that I would like to
automatically add 8 to each calendar month.
 
G

Gadgetman

NO this is not a homework problem. I'm trying to keep track of benefit
hours at work. We get 8 additional hours of sick time every calendar
month. Thank you.
 
P

Pete_UK

If A1 contains the start date, then this should give you the number of
hours, based on 8 per completed calendar month:

=DATEDIF(A1,TODAY(),"m")*8

Hope this helps.

Pete
 
S

Stan Brown

That was my first thought ... but I was unable to find a worksheet
function to calculate months between two dates. Am I missing something?

A first approximation:

=12*(year(A2)-year(A1)) + (month(A2)-month(A1)
 

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

Top