LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Wrong data type returned from user-defined function

Hi All -

I'd appreciate help in writing a function that strips a ROUND "wrapper" from
a formula in another cell. For example, assume cell A3 has a formula such as
=-ROUND(SUM(sales),0) that displays 66.55. I want the put a function in,
say, B3 that is =UnRoundCell(A3), which should display in B3 the unrounded
sum of the range named Sales. Below is a simulated sheet with several other
troublesome examples. The problem is that I don't know how to get the
function to return a formula to the calling cell. In every case it returns a
"dead" text data type in column B rather than an active formula that
produces the proper result.

A B
Comments:

1 =ROUND(66.55,0) =66.55
text -- s/b a number
2 =-ROUND($E$8,0) =-$E$8
text -- s/b a formula that returns

the contents of cell E8
3 =-ROUND(SUM(Sales),0) =-SUM(Sales)
text -- s/b a formula that returns

a number (sum of Sales range)
4 {=ROUND(SUM($B$6:$B$7),0)} =SUM($B$6:$B$7) array
formula in A, but B is plain

non-array text -- s/b an array

formula that returns a sum

Below is the code I've written so far. What have I done wrong? TIA for your
help.

Function UnRoundCell(Cell) As Variant

Dim CellContents As Variant, neg As Boolean
Application.Volatile
CellContents = Cell.Formula

'if formula in cell isn't wrapped by a rounding formula,
' then use existing cell contents unchanged
If Not Left(CellContents, 6) = "=ROUND" And Not Left(CellContents, 7) =
"=-ROUND" Then
UnRoundCell = CellContents
Exit Function
End If

'determine negativity
neg = False
If Left(CellContents, 1) = "-" Or Left(CellContents, 2) = "=-" Then neg
= True
'peel rounding prefix and suffix

If CellContents Like "=-*" Then
CellContents = Right(CellContents, Len(CellContents) - 2 -
Len("Round("))
ElseIf CellContents Like "=*" Then
CellContents = Right(CellContents, Len(CellContents) - 1 -
Len("Round("))
Else
CellContents = Right(CellContents, Len(CellContents) - 0 -
Len("Round("))
End If
CellContents = Left(CellContents, Len(CellContents) - 3)

'add leading sign
If neg Then
CellContents = "=-" & CellContents
Else
CellContents = "=" & CellContents
End If

UnRoundCell = CellContents

End Function


 
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
Dictionary object: Error assigning user defined data type to item Paul Urbanus Excel Programming 2 December 1st 05 04:21 AM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
User-defined data type; Error: Only User-defined types... tiger_PRM Excel Programming 1 July 18th 04 03:32 PM
User defined data type augustus108 Excel Programming 1 April 10th 04 05:11 PM
Passing an Array of User-Defined Type to an Argument of a Function Tushar Mehta[_6_] Excel Programming 0 August 17th 03 06:43 PM


All times are GMT +1. The time now is 08:45 AM.

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"