Need to compare 2 cell values and change 3rd cell if different

arcaidius

Board Regular
Joined
Dec 4, 2018
Messages
97
Good Afternoon,

I have a code to change cell in Column B when A is changed. I would like to also compare this new value in column B to the cell above it, and if different put todays date in column G.
Any Ideas? Here is my code: Have it on Worksheet - Change

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)


Dim aCellColumn As Integer
Dim aTimeColumn As Integer
Dim aRow, aCol As Integer
Dim aDPRg, aRg As Range
aCellColumn = 1
aTimeColumn = 2
aRow = Target.Row
aCol = Target.Column
If Target.Text <> "" Then
    If aCol = aCellColumn Then
       Cells(aRow, aTimeColumn) = Sheets("Main").Range("D2").Value
    Else
        On Error Resume Next
        Set aDPRg = Target.Dependents
        For Each aRg In aDPRg
            If aRg.Column = aCellColumn Then
                Cells(aRg.Row, aTimeColumn) = Sheets("Main").Range("D2").Value
            End If
        Next
    End If
End If
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Well I thought I almost Had it. I got it to update the right cell but it does it whether or not the values are the same.
To explain a little bit, I am entering the month and year as text in column A for my chart labels. Then my code uses the changed cell function to paste a goal amount in Column B from another sheet. What I want to add to this code is if the last entry in column B is different than the previous entry in Column B to put the date in Column G.

I tried to improvise the code above but it is putting the date in column G every time even if the two values are the same. Also it throws up an error 91: Object variable or With block variable not set and highlights this section of code:
VBA Code:
If Cells(aRg.Row, aTimeColumn).Value <> Cells(aRg.Row, aTimeColumn).Offset(-1, 0).Value Then

So here is the whole code below, i added a note where I'm getting the error, could someone please help?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim aCellColumn As Integer
Dim aTimeColumn As Integer
Dim aRow, aCol As Integer
Dim aDPRg, aRg As Range
aCellColumn = 1
aTimeColumn = 2
gTimeColumn = 7
aRow = Target.Row
aCol = Target.Column
If Target.Text <> "" Then
    If aCol = aCellColumn Then
       Cells(aRow, aTimeColumn) = Sheets("Main").Range("D2").Value
    Else
        On Error Resume Next
        Set aDPRg = Target.Dependents
        For Each aRg In aDPRg
            If aRg.Column = aCellColumn Then
                Cells(aRg.Row, aTimeColumn) = Sheets("Main").Range("D2").Value
            End If
        Next
    End If
End If

'Error occurs here
If Cells(aRg.Row, aTimeColumn).Value <> Cells(aRg.Row, aTimeColumn).Offset(-1, 0).Value Then

Cells(aRow, gTimeColumn) = Now()
        On Error Resume Next
        Set aDPRg = Target.Dependents
        For Each aRg In aDPRg
            If aRg.Column = aCellColumn Then
                Cells(aRg.Row, gTimeColumn) = Now()
            End If
Next
End If

End Sub
 
Upvote 0
The line of code on which VBA errors out is outside the For Each aRg In aDPRg loop, so aRg has no reference to any worksheet range at all.
Not quite sure what you want to achieve but I would like to take a look.
 
Upvote 0
Not sure how to attach the work book so here you go:
On this sheet we are entering data each month . The "Date Active" column is when the goal was changed. I made a "Main" Sheet where we can change all the goals for each metric. Trying to Automate as much as possible to save time, its a long arduous process.

So the first Code I posted works fine, when for example Dec 2020 is entered into the next row of column A, The number 3 is pulled from the Main Sheet and put in Column B. What i want to add to the code is that if the goal changes say to 2.5 or whatever, to put a date in the date active column, if its the same do nothing.

FrequencyGoal
Max TRIR
TRIR
Actual
Goal
Max LTIR
LTIR
Actual
Goal Met?Date ActiveDate Measured
Sep 202030.9110Yes08/01/20199/30/2020
Oct 202030.9510Yes08/01/201910/31/2020
Nov 202034.0012Yes11/30/2020
 
Upvote 0
I suspect you are looking for something like this ...

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
   If Not Application.Intersect(Target, Columns("A")) Is Nothing Then
        
        With Target
            If .Count = 1 And Len(.Text) > 0 Then
                .Offset(0, 1) = Sheets("Main").Range("D2").Value
                If .Offset(0, 1).Value <> .Offset(-1, 0).Value Then
                    Application.EnableEvents = False
                    .Offset(0, 6).Value = Date
                    Application.EnableEvents = True
                End If
            End If
        End With
    End If
End Sub
 
Upvote 0
Apologies, made a typo :cry:
The second zero in this line should be a 1
Rich (BB code):
If .Offset(0, 1).Value <> .Offset(-1, 1).Value Then
 
Upvote 0
Solution
You are welcome and glad you like it.
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top