+ Reply to Thread
Results 1 to 4 of 4

VBA Log function Runtime Error '5': Invalid procedure call or argument

Hybrid View

  1. #1
    Registered User
    Join Date
    02-22-2019
    Location
    Louisiana, US
    MS-Off Ver
    2016
    Posts
    2

    VBA Log function Runtime Error '5': Invalid procedure call or argument

    I am trying to use the VBA Log function in an expression and I expect it to work on any number greater than 0. However, when I enter a relatively large number (e.g. 500 or 1,000), I get the error mentioned in the title. Here is the function:

    Function f_x(x As Double) As Double
    
        f_x = 5 - x - Log(x)
    
    End Function
    I am using Option Explicit and I get the error when I enter x as any number greater than about 500.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,518

    Re: VBA Log function Runtime Error '5': Invalid procedure call or argument

    Works just fine for me.
    Function f_x(x As Double) As Double
    
        f_x = 5 - x - Log(x)
    
    End Function
    
    Sub tst()
        Dim ff As Double
        ff = f_x(1000)
        MsgBox ff
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    02-22-2019
    Location
    Louisiana, US
    MS-Off Ver
    2016
    Posts
    2

    Re: VBA Log function Runtime Error '5': Invalid procedure call or argument

    Maybe it has something to do with the sub I'm using the function in then? It gives the error if x is anything above exactly 403 (after further testing) and when I click "Debug" It highlights the line where I defined f_x.

    Full code:
    Option Explicit
    
    Function f_x(x As Double) As Double
        
        f_x = 5 - x - Log(x)
    
    End Function
    
    Function f_prime(x As Double) As Double
    
        f_prime = (-1) - (1 / x)
    
    End Function
    
    Sub Newton_Method()
    
        Dim guess As Double
        Dim tolerance As Double
        Dim next_guess As Double
        Dim iterations As Integer
        
        
        iterations = 1
        guess = Range("B1").Value
        tolerance = Range("B2").Value
        
        next_guess = guess - (f_x(guess) / f_prime(guess))
        
        Do Until (Application.WorksheetFunction.RoundDown(guess, tolerance) = Application.WorksheetFunction.RoundDown(next_guess, tolerance)) Or iterations = 100
        
            guess = next_guess
            next_guess = guess - (f_x(guess) / f_prime(guess))
            iterations = iterations + 1
        
        Loop
        
        Range("B4").Value = next_guess
        Range("B5").Value = iterations
    
    End Sub

  4. #4
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: VBA Log function Runtime Error '5': Invalid procedure call or argument

    Simple enough answer. Numbers above 403 will cause next_guess to go below zero making the Log() call invalid.

    WBD
    Office 365 on Windows 11, looking for rep!

+ 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. Run time error 5, invalid procedure call or argument
    By arvimeld in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-06-2018, 09:11 AM
  2. [SOLVED] Invalid procedure call or argument error
    By maym in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-10-2016, 09:38 PM
  3. [SOLVED] runtime error 5, invalid procedure call or argument ??
    By tintin007 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-01-2014, 05:25 AM
  4. Invalid procedure call or argument (Error 5)
    By vop2311 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-08-2014, 07:27 PM
  5. Runtime Error 5: Invalid procedure call or argument
    By Arasi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-07-2009, 11:49 AM
  6. [SOLVED] Invalid procedure call or argument error
    By Patrick Simonds in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-12-2006, 06:45 PM
  7. Replies: 2
    Last Post: 02-24-2006, 05:30 PM

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