PDA

View Full Version : [SOLVED:] Help With Worksheet Change event



orcas
04-02-2014, 01:54 AM
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

Bob Phillips
04-02-2014, 02:19 AM
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

snb
04-02-2014, 03:27 AM
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

orcas
04-02-2014, 03:51 AM
Thank you both very much! I now have a code that works like a dream!