+ Reply to Thread
Results 1 to 8 of 8

Linking or autocalculation for references in VBA functions

  1. #1
    Registered User
    Join Date
    02-26-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    40

    Linking or autocalculation for references in VBA functions

    See my partial code below. I have made references to a table on a worksheet for various cost assumptions. However if I change an assumption in my table any cells where I have used my VBA function do not reflect the change in assumption. Is there any way I can build into this code a way for that to occur? or do i need to do it in its own seperate sub instead of a function?

    Please Login or Register  to view this content.
    Last edited by Mick S; 03-21-2011 at 07:51 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    Re: Linking or autocalculation for references in VBA functions

    se Named references rather than hard coding the cell references.

    You can probably get away with a refernce to the first group of values and offsets for the others.

    You can limit the number of string tests you do if you say, for example:

    Please Login or Register  to view this content.

    It would help others to help you if you posted a sample workbook with some typical data rather than just the code out of context.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    02-26-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Linking or autocalculation for references in VBA functions

    I would try to post a file but I'd get in big trouble if I posted our cost data externally lol.

    I have already tweaked the string test with an option compare text so that mess is fixed.

    I apologize since I don't think I asked my question in an easy to understand way. My problem is not the references within VBA to a cell (ie Rebates = wks.Range("e23").Value), but its in the cell where I am using my total_cost function where I have the problem.

    If say theres a price of 100, landed cost of 50 and rebates from the price of 10%, my total cost is $60 and it is returned in cell Z10 (where my function is). However if I change the rebate to 20% in Range e23, my function still outputs $60 in z10 unless I hit the f2 key in z10 and then enter. I'd like cell z10 to simply change automatically if I change cell e23's value.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    Re: Linking or autocalculation for references in VBA functions

    Put:

    Please Login or Register  to view this content.

    at the start of the function.

    I think that if your function call refers to a field (cell) that changes, it will recalculate.

    Regards

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Linking or autocalculation for references in VBA functions

    Mick, you should pass all of the parameters required to compute the function's result to the function, both because Excel does not otherwise see any dependency, and because the hard-coded ranges in the function are sure to break someday.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    02-26-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Linking or autocalculation for references in VBA functions

    That did it. Thanks.

  7. #7
    Registered User
    Join Date
    02-26-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Linking or autocalculation for references in VBA functions

    shg -
    I thought about having the parameters within the function itself but there are so many of them and they change on a 6 month basis. It is much easier to update the table in the worksheet that only I have access to (at least for me at this point) than to go back into the VBA code and do it.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Linking or autocalculation for references in VBA functions

    I understand your reluctance, but it's one of a few cardinal sins of programming ...

+ 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