+ Reply to Thread
Results 1 to 3 of 3

Execute macro on cell value change

Hybrid View

  1. #1
    Registered User
    Join Date
    10-22-2014
    Location
    Hyde, Manchester
    MS-Off Ver
    2010
    Posts
    82

    Execute macro on cell value change

    Hi guys I need your help once again!

    I have the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim KeyCells As Range
        Set KeyCells = ActiveSheet.Range("K3:K350")
        
        If Not Application.Intersect(KeyCells, Range(Target.Address)) _
               Is Nothing Then
                MsgBox "I'm In"
                MsgBox ActiveCell.Column
                MsgBox ActiveCell.Row
            If ActiveCell.Value < 0.001 Then
                MsgBox "I'm In Again"
                ActiveCell.NumberFormat = "0.0E+00"
            Else
                ActiveCell.NumberFormat = "0"
            End If
           
        End If
    End Sub
    Its basically just to change the formatting of a cell when the value is entered so that it shows up if the value is very small (the value of the cell can range from 10^6 to 10^-9).

    However when you change the value of the cell, the macro runs off the cell that you selected afterwards (i.e. if you click off the cell then it takes the value of the cell you selected next, or if you press carriage return to leave that cell then it takes the value from the cell below) what is the way to stop it doing this? I tried surrounding my code in an
    Application.EnableEvents
    code but that seemed to make no difference?

    Any help is greatly appreciated

    Regards

    Tom

  2. #2
    Registered User
    Join Date
    11-27-2014
    Location
    Wolverhampton, England
    MS-Off Ver
    2013
    Posts
    49

    Re: Execute macro on cell value change

    Hi Tom,

    I couldn't get it to work by finding the last active cell.

    But I do have an alternative that should do the trick, using a loop to format every cell within the range.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        
    Dim KeyCells As Range
    Set KeyCells = ActiveSheet.Range("K3:K350")
        
    Dim SR As Long
    SR = 3
       
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
    Is Nothing Then
    Do
    If Range("K" & SR).Value < 0.001 Then
    Range("K" & SR).NumberFormat = "0.0E+00"
    Else
    Range("K" & SR).NumberFormat = "0"
    End If
    SR = SR + 1
    Loop Until SR = 351
    End If
        
    End Sub
    Let me know if this works for you!

    Owen

  3. #3
    Registered User
    Join Date
    10-22-2014
    Location
    Hyde, Manchester
    MS-Off Ver
    2010
    Posts
    82

    Re: Execute macro on cell value change

    That worked a treat! Thanks mate

+ 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. Execute a macro in a closed workbook, or open and execute without the user noticing
    By cearaujo123 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-09-2015, 06:44 PM
  2. [SOLVED] Change From Execute on Selections to Execute On All
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-28-2014, 09:23 AM
  3. [SOLVED] ListBox Selections To Change ReportFilter on PivotTable and then Execute Macro
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2014, 09:03 AM
  4. Worksheet_Change Event does not execute upon change of cell
    By KarlaM in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-11-2013, 07:04 PM
  5. Execute Macro When Cells change
    By scottymelloty in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2010, 11:52 AM
  6. On Cell change, execute macro
    By des_wes in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-19-2007, 07:01 PM
  7. [SOLVED] Execute on Cell Change - Problem
    By Marty in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-11-2005, 03:06 PM

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