+ Reply to Thread
Results 1 to 9 of 9

Getting a macro to run automatically when a cell value changes

  1. #1
    Registered User
    Join Date
    04-09-2013
    Location
    Alpharetta, GA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Getting a macro to run automatically when a cell value changes

    I have some code I would like to execute automatically any time the value in C1 is changed:

    Please Login or Register  to view this content.
    I've tried adding

    Please Login or Register  to view this content.
    but no matter how many examples I've looked at I still seem to be missing something because it never runs automatically. What am I doing wrong here?
    Last edited by davesexcel; 04-16-2013 at 08:43 PM. Reason: Code tags required when supplying VBA code, please read the forum rules.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Getting a macro to run automatically when a cell value changes

    Please Login or Register  to view this content.
    Possibly
    Please Login or Register  to view this content.
    The code belongs in the worksheet module

  3. #3
    Registered User
    Join Date
    02-27-2012
    Location
    sydney
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Getting a macro to run automatically when a cell value changes

    may be try this one. put this code in the sheet you want the change to happen

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    If Target.Address = "$C$1" Then
    Application.Run "your code"

    End If
    End Sub
    Last edited by seatejo; 04-16-2013 at 10:03 PM.

  4. #4
    Registered User
    Join Date
    04-09-2013
    Location
    Alpharetta, GA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Getting a macro to run automatically when a cell value changes

    Thanks for the suggestions. I tried the first approach and while the code runs it still forces me to run it manually.

    For the second suggestion I get an error when I run it (and yes it also keeps me running it manually) Compile error: expected function or variable.

    Any other ideas?

  5. #5
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Getting a macro to run automatically when a cell value changes

    Quote Originally Posted by sschluet View Post
    Thanks for the suggestions. I tried the first approach and while the code runs it still forces me to run it manually.........
    Read the last line in my post.
    The code belongs in the worksheet module
    Right click the sheet tab and select view code, you are now in the worksheet module.

    Worksheet Events
    Last edited by davesexcel; 04-17-2013 at 08:17 AM.

  6. #6
    Registered User
    Join Date
    04-09-2013
    Location
    Alpharetta, GA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Getting a macro to run automatically when a cell value changes

    Ok, I moved the code to the worksheet module, changed it so it's not using "sh" and added what you have there:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$1" Then
    If Target = "" Then
    Range("I2:I6").ClearContents
    Else: common2
    End If
    End If
    End Sub

    Sub common2()
    Dim sh As Worksheet, lr As Long, rng As Range, Brng As Range, Crng As Range
    'Set sh = Sheets(6) 'Edit sheet name


    If Range("C1").Value = "" Then
    Range("I2:I6").ClearContents
    Else

    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = Range("A1:A" & lr)
    For Each C In rng
    Set Brng = Range("B:B").Find(C.Value, LookIn:=xlValues)
    Set Crng = Range("C:C").Find(C.Value, LookIn:=xlValues)
    If Not Brng Is Nothing And Not Crng Is Nothing Then
    Cells(Rows.Count, 9).End(xlUp)(2) = C.Value
    End If
    Set Brng = Nothing
    Set Crng = Nothing
    Next

    End If

    End Sub

    It still doesn't run unless I do things manually. I'm still open for ideas.

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Getting a macro to run automatically when a cell value changes

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

  8. #8
    Registered User
    Join Date
    04-09-2013
    Location
    Alpharetta, GA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Getting a macro to run automatically when a cell value changes

    I'm guessing what you want me to do is this:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

    It still doesn't run unless I do things manually. I'm still open for ideas.

  9. #9
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Getting a macro to run automatically when a cell value changes

    Attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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