+ Reply to Thread
Results 1 to 6 of 6

How to run a recorded macro when cell values change?

Hybrid View

Clue_Less How to run a recorded macro... 06-15-2012, 10:41 AM
StevenM Re: How to run a recorded... 06-15-2012, 10:48 AM
Clue_Less Re: How to run a recorded... 06-15-2012, 11:10 AM
RaulRodriguez Re: How to run a recorded... 06-15-2012, 10:51 AM
StevenM Re: How to run a recorded... 06-15-2012, 11:35 AM
Clue_Less Re: How to run a recorded... 06-15-2012, 12:01 PM
  1. #1
    Registered User
    Join Date
    06-11-2012
    Location
    USA
    MS-Off Ver
    Office 97
    Posts
    43

    How to run a recorded macro when cell values change?

    I have a recorded macro that I would like to run automatically when a range of cell values change (say "A1:A5"). How would I code this into VBA?

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: How to run a recorded macro when cell values change?

    In your worksheet module, place the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column = 1 _
        And Target.Row < 6 Then
            ' do something here
        End If
    End Sub

  3. #3
    Registered User
    Join Date
    06-11-2012
    Location
    USA
    MS-Off Ver
    Office 97
    Posts
    43

    Re: How to run a recorded macro when cell values change?

    Quote Originally Posted by StevenM View Post
    In your worksheet module, place the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column = 1 _
        And Target.Row < 6 Then
            ' do something here
        End If
    End Sub
    Would "' do something here" be the macro name? I tried this code and it didn't work.

    Private Sub Worksheet_Change2(ByVal Target As Range)
    Dim KeyCells
    KeyCells = Range("B23:H23")
    If Target.Address = KeyCells Then
        UpdateMash1
    End If
    End Sub
    UpdateMash1 is the name of the recorded macro. I can run the macro via a button and it works, but I can't seem to get it to run automatically when the cells are updated. I'm finally finished with this sheet (thanks to your help with the loop) but now there's a glitch. Cells with formulas that should update automatically when data is entered into cell ranges "B23:H23" won't update unless I scroll out of view of the cells or double click the cells and press enter.

  4. #4
    Forum Contributor
    Join Date
    05-04-2012
    Location
    Stamford,Connecticut,USA
    MS-Off Ver
    Excel 2003
    Posts
    105

    Re: How to run a recorded macro when cell values change?


  5. #5
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: How to run a recorded macro when cell values change?

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("B23:H23")) Is Nothing Then Exit Sub
        ' Run macro here
    End Sub

  6. #6
    Registered User
    Join Date
    06-11-2012
    Location
    USA
    MS-Off Ver
    Office 97
    Posts
    43

    Re: How to run a recorded macro when cell values change?

    Quote Originally Posted by StevenM View Post
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("B23:H23")) Is Nothing Then Exit Sub
        ' Run macro here
    End Sub
    You've done it agian. Thank you Sir!!! Works perfectly. I found another trick while I was googling coding solutions for this. I used Application.ScreenUpdating to eliminate the screen flickering. Now it's like the glitch never happened. Here's the finished code.

    Private Sub Worksheet_Change2(ByVal Target As Range)
        If Intersect(Target, Range("B23:H23")) Is Nothing Then Exit Sub
        Application.ScreenUpdating = False
        UpdateMash1
        Application.ScreenUpdating = True
    End Sub
    Thanks again for all your help.

    Cheers

+ 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