View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\)[_1295_] Rick Rothstein \(MVP - VB\)[_1295_] is offline
external usenet poster
 
Posts: 1
Default DATE IN COMPUTATION

See inline comments...

I tried without success to compute declaringdateas shown below. How
do I declare dates correctly so the formula will compute?


Dim dt, lp1, yb AsDate


The above statement is not doing what you think it is. Only yb is being
declared as aDate; both dt and lp1 are being declared as Variants. In
VB/VBA, you must explicitly declare each variable as to its type. So,
either
do this...

Dim dt AsDate, lp1 AsDate, yb AsDate

or do it this way...

Dim dt AsDate
Dim lp1 AsDate
Dim yb AsDate

dt = DateSerial(2008, 1, 31)
lp1 = DateSerial(2007, 12, 31)
yb = DateSerial(2008, 1, 1)


Range("R" & z).FormulaR1C1 = "=(((RC[-12]+1)-lp1)*RC[-7]*RC[-4]/
36600)+ ((dt-(yb + 1))*RC[-7]*RC[-4]/36600)"


Can't help you with your actual formula as to whether it does what you
want,
but we can help you structure it so that the variables' values are
actually
embedded into it. The reason your formula is not working is that you put
the
variable names **inside** the quote marks where they are just pieces of
text
rather than concatenating the variable names with the surrounding text so
that VBA can see them for the variable that they are. Give this
modification
to your statement a try...

Range("R" & z).FormulaR1C1 = "=(((RC[-12]+1)-" & CStr(lp1) & _
")*RC[-7]*RC[-4]/36600)+ ((" & CStr(dt) & _
"-(" & CStr(yb) & " +
1))*RC[-7]*RC[-4]/36600)"

You might be able to get away without encasing the variable names in the
CStr function calls, but I think it is better to explicitly convert the
contained values into text in order to concatenate them as opposed to
hoping
VBA with get it right by guessing as to how to coerce your variables'
content.

Rick



Thanks Rick for the post. The code ran quite alright but a slight
hitch occurred. It does not produce the correct no. of days within a
given range. For instance,
CStr(dt) & "-RC[-7] gives over 43,000 days instead of 30 days. The
RC[-7] contains the date 01/01/2008

Is there a way to work around this problem? Is the date serial
declaration ok?


I'm not sure what the problem is as I do not recognize what your formula is
attempting to do nor what it expects its inputted arguments to be. If you
can give us a background on "what is what" with it, perhaps someone might be
able to offer specific advice.

Rick