+ Reply to Thread
Results 1 to 7 of 7

Function in VBA and putting it into excel

  1. #1
    Registered User
    Join Date
    04-01-2015
    Location
    Richmond, VA
    MS-Off Ver
    2011
    Posts
    1

    Function in VBA and putting it into excel

    Ok, so here's my code:

    Function LMTD(Tha As Double, Thb As Double, Tcb As Double, Tca As Double) As Double
    LMTD = ((Thb - Tca) - (Tha - Tcb)) / ((Log((Thb - Tca), 2.718) - Log((Tha - Tcb), 2.718)))
    End Function

    and I want to put these values in it:

    Sub Button2_Click()
    Dim z As Double
    z = LMTD(100, 75, 80, 60)
    msgBox z
    End Sub

    but it says that there's a compile error "Identifier under cursor is not recognized" and highlights the sub Button2_Click(). I have a command button that I want to print the z value out to but it a. won't put anything on my excel and b. won't run.
    Please help!!!!

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Function in VBA and putting it into excel

    Try this...

    Please Login or Register  to view this content.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Function in VBA and putting it into excel

    Please use CODE tags around your code

    The only problem that I see is the wrong number of arguments of the Log function.
    Try this

    Please Login or Register  to view this content.
    Here is extract from Help for VBA.Log function which you are using. Note that it's natural log anyway - no need to specify the log base
    Log Function

    Returns a Double specifying the natural logarithm of a number.

    Syntax

    Log(number)

    The required number argument is a Double or any valid numeric expression greater than zero.

    Remarks

    The natural logarithm is the logarithm to the base e. The constant e is approximately 2.718282.

    You can calculate base-n logarithms for any number x by dividing the natural logarithm of x by the natural logarithm of n as follows:

    Logn(x) = Log(x) / Log(n)

    The following example illustrates a custom Function that calculates base-10 logarithms:

    Please Login or Register  to view this content.
    If you want to specify the log base use the example from the help or use WorksheetFunctions.Log


    @Sixthsense: Application doesn't have Log method
    If you are pleased with a member's answer then use the Star icon to rate it.

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Function in VBA and putting it into excel

    Quote Originally Posted by buran View Post
    @Sixthsense: Application doesn't have Log method
    Please check it and let me know if it fails

    We can substitute Worksheetfunction with Application

  5. #5
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Function in VBA and putting it into excel

    Quote Originally Posted by :) Sixthsense :) View Post
    Please check it and let me know if it fails

    We can substitute Worksheetfunction with Application

    Frankly, for so many years I didn't knew that :-)

  6. #6
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Function in VBA and putting it into excel

    Quote Originally Posted by buran View Post
    Frankly, for so many years I didn't knew that :-)
    It 's better to avoid WorksheetFunction and use Application. The below is a simple example I created for your (since you are interested) which will explains the advantage of using Application.

    Run each code and see how application/worksheetfunction handles the error if the match is not found

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Function in VBA and putting it into excel

    Thanks, Sixthsense :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA coding for putting VLookup function
    By aravindkm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-17-2014, 03:16 AM
  2. Replies: 3
    Last Post: 12-15-2013, 04:01 AM
  3. Replies: 7
    Last Post: 06-15-2012, 03:46 PM
  4. creating/putting the same macro in multiple cells with independent function
    By nomad3rider in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-27-2007, 04:01 AM
  5. Putting UDF in custom or predefined function category
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-06-2006, 04:55 AM

Tags for this Thread

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