Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Problem with UDF

I am getting the data in the form of 2/5, 10/18, 5/8 etc.
I need to sum the figures to the left of "/" (slash)and right of "/"
(slash)
To acheive this I am creating two UDF for left and right each.
So far I have this :

Function slsum(kk) As Range
Dim mysum
mysum = 0
For Each cell In kk
'=+LEFT(A2,FIND("/",A2)-1) ----This formula works when entered in cell
pp = WorksheetFunction.Sum(Left(kk, WorksheetFunction.Find("/", kk) -
1))'*ERROR LINE*
mysum = mysum + pp
Next
slsum = mysum
End Function

The error line marked above gives me error of type mismatch.
If I give range as single cell, last line slsum=mysum gives error of
"Object variable or with block variable not set"

Request for solution please.
Regards,
Manakmala.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Problem with UDF

if you have excel 2000 or later

for each cell in kk
varr = split(cell.value,"/")
pp = clng(varr(0)) + clng(varr(1))
Next

--
Regards,
Tom Ogilvy

"mala" wrote in message
om...
I am getting the data in the form of 2/5, 10/18, 5/8 etc.
I need to sum the figures to the left of "/" (slash)and right of "/"
(slash)
To acheive this I am creating two UDF for left and right each.
So far I have this :

Function slsum(kk) As Range
Dim mysum
mysum = 0
For Each cell In kk
'=+LEFT(A2,FIND("/",A2)-1) ----This formula works when entered in cell
pp = WorksheetFunction.Sum(Left(kk, WorksheetFunction.Find("/", kk) -
1))'*ERROR LINE*
mysum = mysum + pp
Next
slsum = mysum
End Function

The error line marked above gives me error of type mismatch.
If I give range as single cell, last line slsum=mysum gives error of
"Object variable or with block variable not set"

Request for solution please.
Regards,
Manakmala.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Problem with UDF

The problem is in how you made the declarations in your function line in
addition to referring to kk as opposed to cell in your pp line. The
Function line should read something like:

Function slsum (kk as Range)

Then in your line where you pick out the value to the left of the slash, you
refer to kk, the range, rather than cell, which is the individual value that
you want to look at. I took the liberty of modifying your code to return a
result appearing in the same format as your input data. I also changed some
variable names for clarity. This is under the assumption that the values
are text values, not decimal values formatted as fractions, which won't
work.

Here's the code:

Option Explicit
Private Function slsum(sumrange As Range)

Dim leftsum As Integer
Dim rightsum As Integer
Dim leftvalue As Integer
Dim rightvalue As Integer
Dim cell As Object

leftsum = 0
rightsum = 0

For Each cell In sumrange
If cell < "" Then
leftvalue = (Left(cell, WorksheetFunction.Find("/", cell) - 1))
'*ERROR LINE*
leftsum = leftsum + Val(leftvalue)
rightvalue = (Right(cell, Len(cell) -
WorksheetFunction.Find("/", cell)))
rightsum = rightsum + Val(rightvalue)
End If
Next
slsum = leftsum & "/" & rightsum

End Function


--
Michael J. Malinsky
Pittsburgh, PA

"I am a bear of very little brain, and long
words bother me." -- AA Milne, Winne the Pooh

"mala" wrote in message
om...
I am getting the data in the form of 2/5, 10/18, 5/8 etc.
I need to sum the figures to the left of "/" (slash)and right of "/"
(slash)
To acheive this I am creating two UDF for left and right each.
So far I have this :

Function slsum(kk) As Range
Dim mysum
mysum = 0
For Each cell In kk
'=+LEFT(A2,FIND("/",A2)-1) ----This formula works when entered in cell
pp = WorksheetFunction.Sum(Left(kk, WorksheetFunction.Find("/", kk) -
1))'*ERROR LINE*
mysum = mysum + pp
Next
slsum = mysum
End Function

The error line marked above gives me error of type mismatch.
If I give range as single cell, last line slsum=mysum gives error of
"Object variable or with block variable not set"

Request for solution please.
Regards,
Manakmala.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Problem with UDF

so bothered he even shortens his own name to Winne
<g


"Michael Malinsky" wrote:


"I am a bear of very little brain, and long
words bother me." -- AA Milne, Winne the Pooh





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Problem with UDF

in addition to Tom's remark:

your function is declared to return a Range object
but i think you just want to return a value...


either:
Function ReturnARange(strAddress as string) as range
SET returnarange = Range(strAddress")
End Function

or:
Function ReturnAValue(kk as range) as Double
kk = application.worksheetfunction.sum(kk)
End Function

Question to Tom:
Any books on comAddins that you can recommend?


keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


(mala) wrote:

I am getting the data in the form of 2/5, 10/18, 5/8 etc.
I need to sum the figures to the left of "/" (slash)and right of "/"
(slash)
To acheive this I am creating two UDF for left and right each.
So far I have this :

Function slsum(kk) As Range
Dim mysum
mysum = 0
For Each cell In kk
'=+LEFT(A2,FIND("/",A2)-1) ----This formula works when entered in cell
pp = WorksheetFunction.Sum(Left(kk, WorksheetFunction.Find("/", kk) -
1))'*ERROR LINE*
mysum = mysum + pp
Next
slsum = mysum
End Function

The error line marked above gives me error of type mismatch.
If I give range as single cell, last line slsum=mysum gives error of
"Object variable or with block variable not set"

Request for solution please.
Regards,
Manakmala.




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
Colon at the end of excel file name(ex: problem.xls:1, problem.xls financeguy New Users to Excel 2 January 15th 10 01:15 AM
Started out as an Access problem. Now an Excel problem RobertM Excel Discussion (Misc queries) 2 April 26th 06 07:30 PM
problem with a conditional max problem Brian Cornejo Excel Discussion (Misc queries) 1 February 18th 05 06:25 PM
Problem when multipple users access shared xl-file at the same time, macrocode for solve this problem? OCI Excel Programming 0 May 16th 04 10:40 PM


All times are GMT +1. The time now is 11:59 PM.

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

About Us

"It's about Microsoft Excel"