+ Reply to Thread
Results 1 to 2 of 2

Run Macro when cell is changed

  1. #1
    Registered User
    Join Date
    06-17-2013
    Location
    Salt Lake City, UT
    MS-Off Ver
    Excel 2011
    Posts
    26

    Run Macro when cell is changed

    I'm trying to have the macro below run when cell D2 is changed. I don't know what code to add to this macro to do that. I also want it so that once the macro runs it then deletes the value in D2 and the cell is left blank and ready for the macro to run again when a new value is entered.


    Sub C3()
    Dim nRw As Long, rCell As Range

    Set rCell = Range("A1")

    If rCell.Value = "" Then
    MsgBox "No Value Found", vbInformation, "Input Required"
    Exit Sub
    End If

    On Error GoTo NoMatchFound
    nRw = Range("A:A").Find(rCell.Value).Row

    With Cells(nRw, "D")
    .Value = .Value + Range("D2").Value
    End With

    Exit Sub

    NoMatchFound:
    MsgBox "No Match Found", vbCritical, "Task Un-Successfull"

    End Sub

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Run Macro when cell is changed

    Install the following Event macro in the worksheet code area:

    Please Login or Register  to view this content.

    Because it is worksheet code, it is very easy to install and automatic to use:
    1. right-click the tab name near the bottom of the Excel window
    2. select View Code - this brings up a VBE window
    3. paste the stuff in and close the VBE window
    If you have any concerns, first try it on a trial worksheet.

    If you save the workbook, the macro will be saved with it.
    If you are using a version of Excel later then 2003, you must save
    the file as .xlsm rather than .xlsx

    To remove the macro:

    1. bring up the VBE windows as above
    2. clear the code out
    3. close the VBE window
    To learn more about macros in general, see:

    http://www.mvps.org/dmcritchie/excel/getstarted.htm

    To learn more about Event Macros (worksheet code), see:

    http://www.mvps.org/dmcritchie/excel/event.htm

    Macros must be enabled for this to work!
    Gary's Student

+ 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