+ Reply to Thread
Results 1 to 11 of 11

VBA Code

  1. #1
    Registered User
    Join Date
    02-09-2005
    Posts
    79

    VBA Code

    I am looking for VBA code by which the results can be obtained without having to run the code. For Instance, if Z = X*Y, I would like the code to automatically calculate Z for as soon as the value of X and Y are changed. I also do not want to use forms. Can someone please help me? Thanks.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Are X and Y values in cells and are these cells fixed?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    02-09-2005
    Posts
    79
    Yes. X and Y are values in Cells and these cells are fixed. Thanks.

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Assuming x and y are in cells A1 and A2, and you want z to be posted in cell A3, you can right-click the sheet tab, select view code, and copy this code:
    Please Login or Register  to view this content.
    HTH

    Jason

  5. #5
    Registered User
    Join Date
    02-09-2005
    Posts
    79
    Thanks, Jason. This works fine. Can you please explain this code and what it does?

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    vioravis,
    Please Login or Register  to view this content.
    This code is lookings to see if two ranges intersect. The first range, Target, is the cell that has been changed on the worksheet. The second range has been defined as the cells that contain your X and Y values. Therefore, if the Target cell is either X or Y, you have an interection and your code fires.

    The If statement is basicly a double negative (Not Nothing) which gives you a positive. If something is Not Nothing then it is Something, is another way of explaining it. If the intersection is nothing then you don't have an intersection so the Else part of the If statement is performed. If the intersection is Not Nothing then you have an intersection and the Then part of your If statement is performed.

    Jason, please correct me if my explanation is incorrect. I am answering this question to make sure my thinking is correct.

    Hope this helps.
    Sincerely,
    Jeff

  7. #7
    Registered User
    Join Date
    02-09-2005
    Posts
    79
    Thanks, boylejob.

    Will it make a difference if I don't have this line there. I removed it and seems to work fine.Also, what should I do if X and Y are in different worksheets?

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Quote Originally Posted by vioravis
    ... if Z = X*Y, I would like the code to automatically calculate Z for as soon as the value of X and Y are changed.... X and Y are values in Cells and these cells are fixed. Thanks.
    If you prefer a UDF.
    Please Login or Register  to view this content.
    can be called from a worksheet cell. If your values are in A1 and A2, the spreadsheet formula
    = myProduct($A$1,$A$2)
    can be put anywhere

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello vioravis,

    This relates to Mike's code. The follow code is Mike's except an additional line has been added. This line ensures that the macro auto updates whenever a cell value that it uses is changed. A volatile function must be recalculated whenever calculation occurs in any cells on the worksheet. A nonvolatile function is recalculated only when the input variables change. This method has no effect if it's not inside a user-defined function used to calculate a worksheet cell.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  10. #10
    Registered User
    Join Date
    02-09-2005
    Posts
    79
    Thanks, mike and leith. I am not sure if I can use the function mike has given since my calculations are quite complicated with lots of variables. I was just trying to present an easier version of my problem with X's and Y's.

    I am trying to use the code given by Jason to copy a text from one sheet to another sheet. The problem with this is I need to double click somewhere in the worksheet to change the results? Can someone please tell me how to avoid this? Thank you.

    Please Login or Register  to view this content.
    Last edited by vioravis; 07-15-2007 at 09:25 PM.

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Put that code in the code module for both sheets.

    The function I posted was also an example. It should be possible to create a UDF that will return the value you want, perhaps with more arguments.

+ 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