+ Reply to Thread
Results 1 to 22 of 22

Macro to run only when a perticular cell value changes

Hybrid View

  1. #1
    Registered User
    Join Date
    07-04-2013
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    16

    Macro to run only when a perticular cell value changes

    Hi Everyone
    So I have a Macro that is only executed when a certain cell value is changed. In my case its (Cell M2) in (sheet 2).
    The value in (Cell M2) is the current year, I used the function =YEAR(NOW()). I want my SaveMe1 macro to run every time a new year begins, aka when the the value in cell M2 changes . I am assuming since I am using the function =YEAR(NOW()), when the next year rolls around it will give me the new year, seeing that it gives me the current year right now.

    PS: I checked the formatting on it and its general, not date or Text or integer. , I even did a quick MsgBox Cells(2, "M") and I get 2013 as a pop display. So far so good.

    For the purpose of testing, since I cant wait an year to see if it actually works, I changed the year function namely, =YEAR(NOW()) to minute function namely, =MINUTE(NOW()). I get my minutes displayed in cell and once again it is formatted as general. I close the workbook and reopen after a few minutes have gone by, and as expected the new minute shows up, but to my disappointment the macro does not get executed.

    now I tried placing the Worksheet_change macro in my ThisWorkbook, nothing
    I tried placing the macro in my sheet2 ,once again nothing. Also The macro that i want to execute is saved under one of the modules.

    Here is the code

    Option Explicit
    I tried the following heading and code
    Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Target.Address
    Case "$M$2"
    'Insert your code here
    Call SaveMe1
    Case Else
    End Select
    And then I tried the following
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Select Case Target.Address
    Case "$M$2"
    'Insert your code here
    Call SaveMe1
    Case Else
    End Select
    End Sub
    Not sure if there is a difference.
    I would appreciate your assistance, I am really frustrated.

    Hopefully I was clear, if not please let me know. Eagerly looking forward to your replies.

    Regards
    Angsome
    Last edited by Angsome; 08-06-2013 at 09:38 AM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to run only when a perticular cell value changes

    Hi Angsome

    This is from http://www.cpearson.com/excel/Events.aspx

    For the Change event in particular, it should be noted that this is triggered when a cell is changed by user action or by other VBA code, but is not raised if the value of a cell is changed as a result of formula calculation.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to run only when a perticular cell value changes

    Hi Angsome

    Place this Code in your Workbook Open Event
    Option Explicit
    Private Sub Workbook_Open()
        With Sheets("Sheet2").Range("M2")
            .Value = Year(Now)
        End With
    End Sub
    You may have an issue if you open the Workbook at Midnight on December 31...

  4. #4
    Registered User
    Join Date
    07-04-2013
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Macro to run only when a perticular cell value changes

    Hi Jaslake
    Thank you for the prompt reply.
    Maybe I should give you more info
    The macro I want to execute when the year changes is a macro that created a backup copy of my existing workbook. So basically its a marco that prompts for SaveAs with a predefined SaveAs name and location. The only issue now is that every time I open the original Workbook the macros prompts for SaveAs, seeing that it assigns Cell M2 with the current year and re-assigns cell M2 with the current year each time I open the Workbook. As far as excel is concerned it considers that as change event and therefore prompts for the SaveAs.
    I only want the macro to run once a year and again when the year changes, in other words when the user opens the workbook again in 2013 nothing happens, since a back up has already been saved, but when he opens the workbook in the new year it prompts the user to save a copy again.

    Is there a way we can do that, hopefully I am not asking for too much.
    I do appreciate your efforts
    Regards
    Angad

    Maybe something like this would help, what do you think
    Option Explicit
    Private Sub Workbook_Open()
        With Sheets("Sheet2").Range("M2")
               If .Value = "" Then
                  .Value = Year(Now)
               Else If .Value < Year(Now) Then
                  .Value = Year(Now)
        End With
    End Sub
    Last edited by Angsome; 08-06-2013 at 09:39 AM. Reason: I had an Idea

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to run only when a perticular cell value changes

    Hi Angad

    Please use Code Tags around your Code...it's a Forum Requirement. To do so, in your Original Post with Code, highlight your code and click the # icon at the top of your post window.

    I'd suggest a similar approach from a slightly different angle.

    I'd type 2013 in Sheet2 Cell M2 then use some Code like this
    Option Explicit
    Private Sub Workbook_Open()
        With Sheets("Sheet2").Range("M2")
            If Not .Value = Year(Now) Then
                .Value = Year(Now)
            End If
        End With
    End Sub
    At the turn of a New Year the Code will change Sheet2 Cell M2 to the New Year and fire your Change Event Code.
    Last edited by jaslake; 08-02-2013 at 04:00 PM.

  6. #6
    Registered User
    Join Date
    07-04-2013
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Macro to run only when a perticular cell value changes

    Hi Jaslake
    As suggested, I did put my code in the proper format and thank you for that.
    Will try your code and see how it goes.
    So far my original code works, but something funny is happening and cant explain.
    What I have is this:

    Option Explicit
    Private Sub Workbook_Open()
    With Sheets("Database").Range("M2")
    'I changed the year function with Minute for testing purpose
        If .Value = "" Then
        .Value = Minute(Now)
        ElseIf .Value < Minute(Now) Then
        .Value = Minute(Now)
      End If
    End With
    'Call my Sort macro
    Sort
    'Save my workbook
    ThisWorkbook.Save
    End If
    End Sub
    The part that I cant explain is the following
    I open the workbook, the macro runs, checks to see if M2 is empty, if so it replaces the empty cell with the current Minute. If its not empty it replaces it with the current Minute and if the minute displayed in the cell is the same minute displayed in the system tray it does nothing. PERFECT

    But randomly if I open the workbook again it does the first part fine and that is to check if it needs to replace the current minute or not in cell M2, but the save macro (SaveMe1) that I have in my Change Module

    Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Target.Address
    Case "$M$2"
    'Insert your code here
    Call SaveMe1
    Case Else
    MsgBox ("nothing")
    End Select
    End Sub
    Sometime runs or sometime does not and in some cases I'll quickly close and reopen the workbook and it goes through the whole procedure just the way it should and that is either run the save macro or not run the macro, depending on the situation. Any thought on why it running fine sometimes and failing sometimes?

  7. #7
    Registered User
    Join Date
    07-04-2013
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Macro to run only when a perticular cell value changes

    Hi Jaslake
    I am sorry for not thanking you earlier. I thought I did.

    But here it goes, Thank you.

    You have been a great help and I cant thank you enough.

    Everything has worked out great.

    Thank you
    Angsome

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Want to find out highest volue in a perticular cell in a different workbooks
    By mahesh1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-03-2013, 12:21 PM
  2. Print/Saving a perticular sheet as PDF
    By 5habbaranks in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 09-13-2011, 12:15 PM
  3. Replies: 11
    Last Post: 09-09-2007, 05:05 AM
  4. Calling a macro automatically when perticular cell gets changed.
    By suhas.nehete in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2007, 07:14 AM
  5. Running macro on the perticular worksheet only
    By vanessa h in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-25-2006, 06:41 AM

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