Consulting

Results 1 to 4 of 4

Thread: Help With Worksheet Change event

  1. #1
    VBAX Regular
    Joined
    Aug 2011
    Posts
    17
    Location

    Help With Worksheet Change event

    Hi there,

    Thanks for taking a look. I am having some problems with a worksheet change event. What i want to be able to achieve is run code whenever a cell in a certain column changes but the code should only run if the values in the cell does not equal values in an adjacent cell.

    So for instance, run macro if cells [B1] changes, but only if values in cells [B1] <> values in cell [C1]

    I will appreciate any help with this.

    Thank you.


    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell_to_test As Range, Cell_changed As Range
    Set Cell_changed = Target(1, 1)
    Set cell_to_test = Range("H20:H700")
    If Not Intersect(Cell_changed, cell_to_test) Is Nothing Then
        
        For i = 1 To 20
            If Cells(i, 1) <> Cells(i, 2) Then
            LossParameter.LossParam
            ActiveSheet.Rows(Target.Row).Interior.Color = vbYellow
            End If
        Next i
            
     
             
    End If
    End Sub

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim cell_to_test As Range, Cell_changed As Range
        
        On Error GoTo ws_exit
        
        Application.EnableEvents = False
        
        Set Cell_changed = Target(1, 1)
        Set cell_to_test = Range("H20:H700")
        
        If Not Intersect(Cell_changed, cell_to_test) Is Nothing Then
             
            If Cell_changed.Value <> Cell_changed.Offset(0, 1).Value Then
            
                LossParameter.LossParam
                Cell_changed.EntireRow.Interior.Color = vbYellow
            End If
        End If
    
    ws_exit:
        Application.EnableEvents = True
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    or
    Private Sub Worksheet_Change(ByVal Target As Range) 
        If Not Intersect(target, Range("H20:H700")) Is Nothing Then 
            If target.value <> target.Offset(0, 1).Value Then 
                LossParameter.LossParam 
                target.EntireRow.Interior.Color = vbYellow 
            End If 
        End If 
    End Sub

  4. #4
    VBAX Regular
    Joined
    Aug 2011
    Posts
    17
    Location
    Thank you both very much! I now have a code that works like a dream!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •