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


Using ExcHi All,
I am trying to calculate the area of a polygon, (using a function
kindly posted by somebody on this newsgroup) .
I am not very familiar with the required syntax ofarrays and
functions. The function should be executed under a forms commandbutton
as follows:

Sub area()
Range("AD5:AE5").Select
Range("a5").Value = AreaByCoordinates((Selection())) ' THIS IS WHERE
THE PROBLEM LIES
End Sub

Function AreaByCoordinates(Xcoord() As Double, _
Ycoord() As Double) As Double
Dim I As Long
Dim Xold As Double
Dim Yold As Double
Dim Yorig As Double
Dim ArrayUpBound As Long
ArrayUpBound = UBound(Xcoord)
Xold = Xcoord(ArrayUpBound)
Yorig = Ycoord(ArrayUpBound)
Yold = 0#
For I = LBound(Xcoord) To ArrayUpBound
x = Xcoord(I)
y = Ycoord(I) - Yorig
AreaByCoordinates = AreaByCoordinates + _
(Xold - x) * (Yold + y)
Xold = x
Yold = y
Next
AreaByCoordinates = Abs(AreaByCoordinates) / 2
End Function

I get the message "Type mismatch: Array or userdefined type expected"

(Using Excel 2003)

Can anybody assist please?
Thanks very much.
Pierre Henning
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default FUNCTION SYNTAX

Well, what I see is that you're calling the function with a single argument,
but the function is expecting two arguments, each a type Double. I'm sure
what happened is that you believed 1) Selection() was the right way to refer
to the two cells that you already created, and 2) a range object containing
two cells would be the syntactic equivalent of the values of those two cells
separately. The first may be true, I'm not sure; I always specify the parent
object, say ActiveSheet.Selection. But I'm certain the second is a mistake.
If the X and Y coordinates are contained in AD5 and AE5, you're going to have
to make the call this way:

Range("A5").Value = AreaByCoordinates(Range("AD5"), Range("AE5"))

But even then I'm not sure of the default parent; are you sure you don't
have to specify one? I would do it like this:

With ActiveSheet
.Range("A5").Value = AreaByCoordinates(.Range("AD5"), .Range("AE5"))
End With

That passes two cell objects to AreaByCoordinates, which is expected not
Cells but Doubles, so even that may not be exact enough, but anyway it's
closer to what AreaByCoordinates has been told to demand.

--- "Pierre" wrote:
I am trying to calculate the area of a polygon, (using a function kindly posted by
somebody on this newsgroup). I am not very familiar with the required syntax
of arrays and functions. The function should be executed under a forms
commandbutton as follows:

Sub area()
Range("AD5:AE5").Select
Range("a5").Value = AreaByCoordinates((Selection())) ' THIS IS WHERE
THE PROBLEM LIES
End Sub

Function AreaByCoordinates(Xcoord() As Double, _
Ycoord() As Double) As Double
Dim I As Long
Dim Xold As Double
Dim Yold As Double
Dim Yorig As Double
Dim ArrayUpBound As Long
ArrayUpBound = UBound(Xcoord)
Xold = Xcoord(ArrayUpBound)
Yorig = Ycoord(ArrayUpBound)
Yold = 0#
For I = LBound(Xcoord) To ArrayUpBound
x = Xcoord(I)
y = Ycoord(I) - Yorig
AreaByCoordinates = AreaByCoordinates + _
(Xold - x) * (Yold + y)
Xold = x
Yold = y
Next
AreaByCoordinates = Abs(AreaByCoordinates) / 2
End Function

I get the message "Type mismatch: Array or userdefined type expected"

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
Indirect function syntax Matt Excel Discussion (Misc queries) 4 October 9th 08 10:43 PM
Syntax for IF function Sarah New Users to Excel 1 July 31st 06 01:57 PM
Shell Function - Syntax bill_morgan Excel Programming 2 September 15th 05 02:03 AM
NetWorkDays Function Syntax Lamar Excel Worksheet Functions 2 April 12th 05 10:02 AM
VBA Function syntax Forrest[_3_] Excel Programming 5 December 17th 03 02:14 AM


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