+ Reply to Thread
Results 1 to 6 of 6

How do get a macro to autorun when i change a cell in the spreadsheet?

  1. #1
    Registered User
    Join Date
    03-23-2006
    Location
    Malaysia
    Posts
    47

    Question How do get a macro to autorun when i change a cell in the spreadsheet?

    Let say Cell "A1" has only 2 values that is Purchase or Sales.

    Now when i change this cell to either value, i want it to run a macro

    e.g.
    When i change Cell to "Purchase" i want to autorun Macro A
    And
    When i change Cell to "Sales" i want to autorun Macro B

    How do i do this? And it is for a particular worksheet only.

    Thanks in advance.

  2. #2
    Jim Thomlinson
    Guest

    RE: How do get a macro to autorun when i change a cell in the spreadsh

    Right click the tab of the sheet containing the ceel with purchase and sale
    in Cell A1 then select view code... Paste the following.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then
    If Target.Value = "Purchase" Then Call Macro1
    If Target.Value = "Sales" Then Call Macro2
    End If
    End Sub
    --
    HTH...

    Jim Thomlinson


    "Infinity" wrote:

    >
    > Let say Cell "A1" has only 2 values that is Purchase or Sales.
    >
    > Now when i change this cell to either value, i want it to run a macro
    >
    > e.g.
    > When i change Cell to "Purchase" i want to autorun Macro A
    > And
    > When i change Cell to "Sales" i want to autorun Macro B
    >
    > How do i do this? And it is for a particular worksheet only.
    >
    > Thanks in advance.
    >
    >
    > --
    > Infinity
    > ------------------------------------------------------------------------
    > Infinity's Profile: http://www.excelforum.com/member.php...o&userid=32725
    > View this thread: http://www.excelforum.com/showthread...hreadid=526617
    >
    >


  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    You need to use the worksheet change event

    This goes in the module for the particular worksheet that you want to trigger the macro

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$2" Then
    Application.EnableEvents = False
    Select Case Target.Value
    Case "Purchase"
    Call macroA
    Case "Sales"
    Call MacroB
    End Select
    End If
    Application.EnableEvents = False
    End Sub

  4. #4
    Registered User
    Join Date
    03-23-2006
    Location
    Malaysia
    Posts
    47

    Talking

    That was quick... Thanks guys. Will try it out now.

  5. #5
    Greg Wilson
    Guest

    Re: How do get a macro to autorun when i change a cell in the spre

    Mudraker had a slight oversight. The last line of mudraker's code should be:
    Application.EnableEvents = True

    Regards,
    Greg

    "Infinity" wrote:

    >
    > That was quick... Thanks guys. Will try it out now.
    >
    >
    > --
    > Infinity
    > ------------------------------------------------------------------------
    > Infinity's Profile: http://www.excelforum.com/member.php...o&userid=32725
    > View this thread: http://www.excelforum.com/showthread...hreadid=526617
    >
    >


  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Greg

    Thanks for picking up my mistake - silly me forgot to change setting from False to True after copying & pasting that line of code

+ 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