+ Reply to Thread
Results 1 to 6 of 6

Custom functions using arguments with same name

  1. #1
    Spencer Hutton
    Guest

    Custom functions using arguments with same name

    I have written some functions in VBA to calculate profit margin, cost and
    suggested retail price. they are simple in that the RETAILPRICE function has
    two arguments, COST and MARGIN. i am also using an argument named COST in
    the PROFITMARGIN function. how can i get VBA to recognize these as seperate
    variables? do i have to put them in separate modules? or is there a certain
    way i need to declare the variables? thank you.

  2. #2
    Tom Ogilvy
    Guest

    Re: Custom functions using arguments with same name

    Public Function RetailPrice(arg1)
    Dim Cost as Double
    end Function

    Public function ProfitMargin(arg1)
    Dim Cost as Double
    End Function

    the value of cost in either function is unrelated to the value of cost in
    the other function. The same for Arg1

    --
    Regards,
    Tom Ogilvy


    "Spencer Hutton" <SpencerHutton@discussions.microsoft.com> wrote in message
    news:B87FC5F5-0A32-4E34-9E64-1F00128FBD72@microsoft.com...
    > I have written some functions in VBA to calculate profit margin, cost and
    > suggested retail price. they are simple in that the RETAILPRICE function

    has
    > two arguments, COST and MARGIN. i am also using an argument named COST in
    > the PROFITMARGIN function. how can i get VBA to recognize these as

    seperate
    > variables? do i have to put them in separate modules? or is there a

    certain
    > way i need to declare the variables? thank you.




  3. #3
    Charlie
    Guest

    RE: Custom functions using arguments with same name

    Private Sub RetailPrice(Cost As Double, Margin As Double)
    End Sub
    Private Sub ProfitMargin(Cost As Double)
    End Sub

    In these two subs the args "Cost" are local variables. They are visible
    only to the code within the routines. You don't have to use the same
    variable name when you call the routines. You can call them like this:

    Dim RetailCost As Double
    Dim MarginCost As Double
    Dim Margin As Double

    Call RetailPrice(RetailCost, Margin)
    Call ProfitMargin(MarginCost)

    ....and two different values will be passed into the two routines as "Cost"
    when they arrive. Clear as mud?

    "Spencer Hutton" wrote:

    > I have written some functions in VBA to calculate profit margin, cost and
    > suggested retail price. they are simple in that the RETAILPRICE function has
    > two arguments, COST and MARGIN. i am also using an argument named COST in
    > the PROFITMARGIN function. how can i get VBA to recognize these as seperate
    > variables? do i have to put them in separate modules? or is there a certain
    > way i need to declare the variables? thank you.


  4. #4
    Spencer Hutton
    Guest

    Re: Custom functions using arguments with same name

    what happens if i want to use something called RetailPrice as an argument in
    another function.

    "Tom Ogilvy" wrote:

    > Public Function RetailPrice(arg1)
    > Dim Cost as Double
    > end Function
    >
    > Public function ProfitMargin(arg1)
    > Dim Cost as Double
    > End Function
    >
    > the value of cost in either function is unrelated to the value of cost in
    > the other function. The same for Arg1
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "Spencer Hutton" <SpencerHutton@discussions.microsoft.com> wrote in message
    > news:B87FC5F5-0A32-4E34-9E64-1F00128FBD72@microsoft.com...
    > > I have written some functions in VBA to calculate profit margin, cost and
    > > suggested retail price. they are simple in that the RETAILPRICE function

    > has
    > > two arguments, COST and MARGIN. i am also using an argument named COST in
    > > the PROFITMARGIN function. how can i get VBA to recognize these as

    > seperate
    > > variables? do i have to put them in separate modules? or is there a

    > certain
    > > way i need to declare the variables? thank you.

    >
    >
    >


  5. #5
    Tom Ogilvy
    Guest

    Re: Custom functions using arguments with same name

    You mean you want to have function named retailprice and a variable named
    retailprice. Without testing, I suspect you will get an error in the place
    where you use it as a variable if the function definition is within scope.
    Sounds like a bad idea to me. Why not

    fncRetailprice
    varRetailprice

    or something like that.

    --
    Regards,
    Tom Ogilvy


    "Spencer Hutton" <SpencerHutton@discussions.microsoft.com> wrote in message
    news:1B995BC4-ADFD-431F-9B7F-A376DA145C00@microsoft.com...
    > what happens if i want to use something called RetailPrice as an argument

    in
    > another function.
    >
    > "Tom Ogilvy" wrote:
    >
    > > Public Function RetailPrice(arg1)
    > > Dim Cost as Double
    > > end Function
    > >
    > > Public function ProfitMargin(arg1)
    > > Dim Cost as Double
    > > End Function
    > >
    > > the value of cost in either function is unrelated to the value of cost

    in
    > > the other function. The same for Arg1
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > > "Spencer Hutton" <SpencerHutton@discussions.microsoft.com> wrote in

    message
    > > news:B87FC5F5-0A32-4E34-9E64-1F00128FBD72@microsoft.com...
    > > > I have written some functions in VBA to calculate profit margin, cost

    and
    > > > suggested retail price. they are simple in that the RETAILPRICE

    function
    > > has
    > > > two arguments, COST and MARGIN. i am also using an argument named

    COST in
    > > > the PROFITMARGIN function. how can i get VBA to recognize these as

    > > seperate
    > > > variables? do i have to put them in separate modules? or is there a

    > > certain
    > > > way i need to declare the variables? thank you.

    > >
    > >
    > >




  6. #6
    Spencer Hutton
    Guest

    Re: Custom functions using arguments with same name

    that's a better idea, i'll try it thanks.

    "Tom Ogilvy" wrote:

    > You mean you want to have function named retailprice and a variable named
    > retailprice. Without testing, I suspect you will get an error in the place
    > where you use it as a variable if the function definition is within scope.
    > Sounds like a bad idea to me. Why not
    >
    > fncRetailprice
    > varRetailprice
    >
    > or something like that.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "Spencer Hutton" <SpencerHutton@discussions.microsoft.com> wrote in message
    > news:1B995BC4-ADFD-431F-9B7F-A376DA145C00@microsoft.com...
    > > what happens if i want to use something called RetailPrice as an argument

    > in
    > > another function.
    > >
    > > "Tom Ogilvy" wrote:
    > >
    > > > Public Function RetailPrice(arg1)
    > > > Dim Cost as Double
    > > > end Function
    > > >
    > > > Public function ProfitMargin(arg1)
    > > > Dim Cost as Double
    > > > End Function
    > > >
    > > > the value of cost in either function is unrelated to the value of cost

    > in
    > > > the other function. The same for Arg1
    > > >
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > >
    > > > "Spencer Hutton" <SpencerHutton@discussions.microsoft.com> wrote in

    > message
    > > > news:B87FC5F5-0A32-4E34-9E64-1F00128FBD72@microsoft.com...
    > > > > I have written some functions in VBA to calculate profit margin, cost

    > and
    > > > > suggested retail price. they are simple in that the RETAILPRICE

    > function
    > > > has
    > > > > two arguments, COST and MARGIN. i am also using an argument named

    > COST in
    > > > > the PROFITMARGIN function. how can i get VBA to recognize these as
    > > > seperate
    > > > > variables? do i have to put them in separate modules? or is there a
    > > > certain
    > > > > way i need to declare the variables? thank you.
    > > >
    > > >
    > > >

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1