Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA Function won't work!

Can anybody please enlighten me why this Excel VBA function* works:

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1)
End Function

and why this one does not (it alwasy returns a #VALUE! error):

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1) * 2
End Function

Help, would greatly appreciated before insanity sets in.




* this function, by the way, is supposed to find the top right hand
value in any excel range. So if, in Excel, you did =topright(A1:F5),
the function would return the value in A5.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default VBA Function won't work!

The first function is actually returning an array of the same
dimensions as the input range, with the top right cell of the
input as its top left; so it just happens to display the right
value. To fix the second function you need to make sure
you pick the single cell you're interested in. The line:

topright = values.Offset(0, values.Columns.Count - 1).Cells(1, 1).2

will do it.

Andrew

wrote:
Can anybody please enlighten me why this Excel VBA function* works:

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1)
End Function

and why this one does not (it alwasy returns a #VALUE! error):

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1) * 2
End Function

Help, would greatly appreciated before insanity sets in.




* this function, by the way, is supposed to find the top right hand
value in any excel range. So if, in Excel, you did =topright(A1:F5),
the function would return the value in A5.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default VBA Function won't work!

Piotr - many many thanks for the quick reply. I understand what you are
doing in your function (-checking for non numeric values), but that was
not what I had meant to find out.

What I am trying to find out is why, when there *is* a numeric value in
the top right hand cell of the "values" range, why can it be not
multiplied without creating an error? (i.e. I can actually multiply the
result of the topright() function in Excel and get the right answer,
but I cannot do that inside the VBA fucntion itself without creating an
error). Any ideas? Many thanks as always!



Piotr Lipski wrote:
On 29 Jun 2006 00:15:07 -0700, wrote:

Can anybody please enlighten me why this Excel VBA function* works:

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1)
End Function

and why this one does not (it alwasy returns a #VALUE! error):

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1) * 2
End Function


Public Function TopRight(ByRef rngValues As Excel.Range) As Variant
Dim varTmp As Variant
On Error GoTo tr_err
VarTmp = values.Offset(0, values.Columns.Count - 1)
If(IsNumeric(VarTmp)) Then
TopRight = values.Offset(0, values.Columns.Count - 1) * 2
Else
TopRight = Null 'or 0 or "err" or ...
End If

tr_ex:
Exit Function

tr_err:
TopRight = Null 'or 0 or "err" or...
Resume tr_ex
End Function


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default VBA Function won't work!

Andrew - thank you v much - you hit the nail on th ehead (i did not
know it returned an array!). Piotr, thank you too for your input!
- Chris


Andrew Taylor wrote:
The first function is actually returning an array of the same
dimensions as the input range, with the top right cell of the
input as its top left; so it just happens to display the right
value. To fix the second function you need to make sure
you pick the single cell you're interested in. The line:

topright = values.Offset(0, values.Columns.Count - 1).Cells(1, 1).2

will do it.

Andrew

wrote:
Can anybody please enlighten me why this Excel VBA function* works:

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1)
End Function

and why this one does not (it alwasy returns a #VALUE! error):

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1) * 2
End Function

Help, would greatly appreciated before insanity sets in.




* this function, by the way, is supposed to find the top right hand
value in any excel range. So if, in Excel, you did =topright(A1:F5),
the function would return the value in A5.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 747
Default VBA Function won't work!

Try:

Function topright(values As Range) As Double
topright = values(1, values.Columns.Count) * 2
End Function

The reason your function doesn't work is because the Offset method returns a
range of cells and your function is tyring to multiply an array by a single
element (i.e. 2).

Regards,
Greg
" wrote:

Can anybody please enlighten me why this Excel VBA function* works:

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1)
End Function

and why this one does not (it alwasy returns a #VALUE! error):

Function topright(values)
topright = values.Offset(0, values.Columns.Count - 1) * 2
End Function

Help, would greatly appreciated before insanity sets in.




* this function, by the way, is supposed to find the top right hand
value in any excel range. So if, in Excel, you did =topright(A1:F5),
the function would return the value in A5.


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
Function doesn't work Neal Carron[_2_] Excel Worksheet Functions 2 January 26th 10 02:11 AM
need a function that will work using multiple work books and sheet capt c Excel Worksheet Functions 1 March 30th 09 10:20 PM
Why does my Function not work? [email protected] Excel Discussion (Misc queries) 2 April 27th 07 10:22 PM
NZ function doesn't work! salut Excel Programming 2 March 23rd 06 07:46 PM
Why it can work in Function but ok in Sub new.microsoft.com Excel Programming 1 November 21st 05 09:44 AM


All times are GMT +1. The time now is 08:26 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"