Results 1 to 12 of 12

executing a sub within a function

Threaded View

moosemaster23 executing a sub within a... 02-01-2013, 09:05 AM
JosephP Re: executing a sub within a... 02-01-2013, 09:08 AM
arlu1201 Re: executing a sub within a... 02-01-2013, 09:11 AM
moosemaster23 Re: executing a sub within a... 02-01-2013, 09:11 AM
MrShorty Re: executing a sub within a... 02-01-2013, 11:25 AM
moosemaster23 Re: executing a sub within a... 02-01-2013, 11:33 AM
Norie Can't making_f be converted... 02-01-2013, 11:47 AM
moosemaster23 Re: executing a sub within a... 02-01-2013, 11:49 AM
MrShorty Re: executing a sub within a... 02-01-2013, 12:59 PM
moosemaster23 Re: executing a sub within a... 02-03-2013, 07:11 AM
moosemaster23 Re: executing a sub within a... 02-03-2013, 07:52 AM
MrShorty Re: executing a sub within a... 02-03-2013, 11:19 AM
  1. #1
    Registered User
    Join Date
    02-01-2013
    Location
    Haywards Heath
    MS-Off Ver
    Excel 2010
    Posts
    24

    executing a sub within a function

    Hi guys, I'm new here.

    I'm trying to write some code which will take a polynomial and output it's value for varying x.

    For a given input "n" I contruct n+1 placeholders (1, x, x^2, ... , x^n)

    Beneath them I put in values

    Then for a given "x" (in cells (3,2)).

    I run the following code to get a value for f(x) (which is put in cell (9,1)).

    Function f_1(n, x)
    
    f_1 = x ^ n
    
    End Function


    Sub making_f()
    
    x = Worksheets(2).Cells(3, 2).Value
    'this is the input for x
    n = Worksheets(2).Cells(1, 2).Value
    'this is the degree of the polynomial
    
    Worksheets(2).Cells(8, 2) = Worksheets(2).Cells(7, 2).Value
    'this is dropping down the constant term to the next row
    For m = 1 To n
    
    Worksheets(2).Cells(8, 2 + m) = Worksheets(2).Cells(7, m + 2) * f_1(m, x)
    Next
    'this is inputting the values of constant*x^m in the next row
    
    Worksheets(2).Cells(9, 2) = Worksheets(2).Cells(7, 2).Value
    
    'this is dropping the constant term down again as I don't know how to sum efficiently in vba I do it iteratively.
    For m = 1 To n
    
    Worksheets(2).Cells(9, m + 2) = Worksheets(2).Cells(9, m + 1).Value + Worksheets(2).Cells(8, m + 2).Value
    'this iterates the summation
    Next
    
    Worksheets(2).Cells(9, 1) = Worksheets(2).Cells(9, n + 2).Value
    'this gives me the value of f(x)
    End Sub

    Now the problem is that I want to make the function f.

    I tried the following to no avail:

    Function f(x)
    
    Worksheets(2).Cells(3, 2).Value = x
    making_f
    f = Worksheets(2).Cells(9, 1).Value
    
    
    End Function

    I just get a value error.

    Any ideas? P.S. Sorry if my formatting is awful, I'm new to presenting code on forums.
    Last edited by arlu1201; 02-01-2013 at 09:11 AM. Reason: Use code tags as per forum rules.

Thread Information

Users Browsing this Thread

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

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