Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Custom formula

Hello, I have a sub which copies a formula to 14 columns. Each column
formula has a calculated range above the cell where the formula is placed.
The declaration of the formula is as follows.

Public Function TotalWorkTime(unit As Long, rgnAdd As String) As Double
'here i try to set a new range to the address of rgnAdd
Set rgn2 = Range( rgn )

The routine which copies the formula into the desired cells is
dim rangeAddress as string

For x = 3 To 16
Set rgn = Range(Cells(rowFrom, x), Cells(rowTo, x))
rangeAddress = rgn.Address(RowAbsolute:=False, ColumnAbsolute:=False)
Cells(rowOnUnitsPage - 2, x).Formula = "=TotalWorkTime(" _
& unit & "," & rangeAddress & ")"
Next x

With the above code the formula in the formula bar is correct and the range
adjusts whenever I insert or delete a row. Unfortunately, I get a #Value
error in the cell. When debugging, I noticed the value of rgnAdd is missing
when the program enters the TotalWorkTime function. Therefore rgn2 is never
set and the sub is exited without execution.
If I send the function rgnAdd like the following
Cells(rowOnUnitsPage - 2, x).Formula = "=TotalWorkTime(" _
& unit & "," & Chr(34) & rangeAddress & Chr(34) & ")"

I get the correct value calculated but the range will not change as I insert
or delete rows.
Any thoughts would be greatly appreciated
thanks.
billQ


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Custom formula

I think that your problem is due to Excel not being able to convert
the code into a formula correctly in one go. Try converting your
formula to a string in an intermediate step, and use that (when you
can also check it is correct - it looks suspect to me). I am unable to
test your code because there is no indication of what variables are
produced.

Don't try to do too much in one line of code. Break the process down
into more simple, easily managed, checkable steps.

Dim MyRange as Range
Dim MyFormula As String
'-----------------------
Set MyRange = ......................
MsgBox(MyRange.Address)

MyFormula = "=TotalWorkTime(" _
& unit & "," & Chr(34) & rangeAddress & Chr(34) & ")"
MsgBox (MyString)

*To use Excel's ability to make the formula relative we have to put it
into the whole range in one go :-

MyRange.Formula = MyFormula


Regards
BrianB
================================================== ======







"billQ" wrote in message ...
Hello, I have a sub which copies a formula to 14 columns. Each column
formula has a calculated range above the cell where the formula is placed.
The declaration of the formula is as follows.

Public Function TotalWorkTime(unit As Long, rgnAdd As String) As Double
'here i try to set a new range to the address of rgnAdd
Set rgn2 = Range( rgn )

The routine which copies the formula into the desired cells is
dim rangeAddress as string

For x = 3 To 16
Set rgn = Range(Cells(rowFrom, x), Cells(rowTo, x))
rangeAddress = rgn.Address(RowAbsolute:=False, ColumnAbsolute:=False)
Cells(rowOnUnitsPage - 2, x).Formula = "=TotalWorkTime(" _
& unit & "," & rangeAddress & ")"
Next x

With the above code the formula in the formula bar is correct and the range
adjusts whenever I insert or delete a row. Unfortunately, I get a #Value
error in the cell. When debugging, I noticed the value of rgnAdd is missing
when the program enters the TotalWorkTime function. Therefore rgn2 is never
set and the sub is exited without execution.
If I send the function rgnAdd like the following
Cells(rowOnUnitsPage - 2, x).Formula = "=TotalWorkTime(" _
& unit & "," & Chr(34) & rangeAddress & Chr(34) & ")"

I get the correct value calculated but the range will not change as I insert
or delete rows.
Any thoughts would be greatly appreciated
thanks.
billQ

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Custom formula

thanks for the help Brian, I change the formula to a string and it works
like a champ
billQ
"billQ" wrote in message
...
Hello, I have a sub which copies a formula to 14 columns. Each column
formula has a calculated range above the cell where the formula is placed.
The declaration of the formula is as follows.

Public Function TotalWorkTime(unit As Long, rgnAdd As String) As Double
'here i try to set a new range to the address of rgnAdd
Set rgn2 = Range( rgn )

The routine which copies the formula into the desired cells is
dim rangeAddress as string

For x = 3 To 16
Set rgn = Range(Cells(rowFrom, x), Cells(rowTo, x))
rangeAddress = rgn.Address(RowAbsolute:=False, ColumnAbsolute:=False)
Cells(rowOnUnitsPage - 2, x).Formula = "=TotalWorkTime(" _
& unit & "," & rangeAddress & ")"
Next x

With the above code the formula in the formula bar is correct and the

range
adjusts whenever I insert or delete a row. Unfortunately, I get a #Value
error in the cell. When debugging, I noticed the value of rgnAdd is

missing
when the program enters the TotalWorkTime function. Therefore rgn2 is

never
set and the sub is exited without execution.
If I send the function rgnAdd like the following
Cells(rowOnUnitsPage - 2, x).Formula = "=TotalWorkTime(" _
& unit & "," & Chr(34) & rangeAddress & Chr(34) & ")"

I get the correct value calculated but the range will not change as I

insert
or delete rows.
Any thoughts would be greatly appreciated
thanks.
billQ




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use a formula in custom autofilter andy62 Excel Worksheet Functions 6 April 24th 23 09:01 PM
custom field with custom formula in pivot table! Sedaghat Nejad Excel Worksheet Functions 2 June 21st 09 11:19 AM
custom add formula Kelly******** Excel Worksheet Functions 7 January 13th 08 08:10 AM
Need Custom format, not a formula nastech Excel Discussion (Misc queries) 21 April 29th 06 03:42 AM
Custom defined formula billQ Excel Programming 2 July 15th 03 08:44 AM


All times are GMT +1. The time now is 10:50 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"