Defining the cell that causes a Worksheet Change Event

HJA14

Board Regular
Joined
Apr 12, 2016
Messages
60
Hi all,

I have a question regarding the third answer on the following topic: https://stackoverflow.com/questions...-the-old-value-of-a-changed-cell-in-excel-vba

I have written the following macro:


Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim newPrice, oldPrice As String
    Dim cell, intersection As Range
    
    For Each cell In Target
        If Not Intersect(cell, Range("B:B")) Is Nothing Then
            newPrice = cel.Value
            oldPrice = ThisWorkbook.Worksheets("HiddenSheet").Range(cell.Address).Value
            Call Tick(oldPrice, newPrice)
        End If
    
    Next cell
    
    ThisWorkbook.Worksheets("HiddenSheet").UsedRange.Clear
    Me.UsedRange.Copy ThisWorkbook.Worksheets("HiddenSheet").Range(Me.UsedRange.Address)
    
End Sub

Q: I know how "Calling" works, but what does exactly the ..(oldPrice, newPrice)-part of the Calling? Are these values remembered when running the Tick-macro?

The Tick-macro does the following:

Code:
Sub Tick()    
    Dim newPrice, oldPrice As String
    Cells(Target.Row, 4).Value = oldPrice
    
    If Cells(Target.Row, Target.Column).Value < Cells(Target.Row, 4).Value Then
        Cells(Target.Row, 2).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 10066431
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        Cells(Target.Row, 10).Value = Cells(Target.Row, 10).Value + 1
            
    Else
        Cells(Target.Row, 2).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 11919289
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        Cells(Target.Row, 9).Value = Cells(Target.Row, 9).Value + 1
        
    End If


End Sub

However, how do I correctly replace the Target-part with the location of the cell from the "For Each cell In Target" in the previous macro?
For exampe, "Cells(Target.Row, 4).Value = oldPrice" should be rewritten to?

Thanks in advance,
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Q: I know how "Calling" works, but what does exactly the ..(oldPrice, newPrice)-part of the Calling? Are these values remembered when running the Tick-macro?

Your code is passing two arguments to your Tick procedure EXCEPT you have not declared the arguments in the procedure.

Also, you have declared your variables with different data types

Code:
Dim newPrice, oldPrice As String

newPrice is declared as variant (default) which is ok ish whilst oldPrice is declared as string.

I suspect you want Currency? if so, best to declare required data types to avoid errors

Code:
Dim newPrice As Currency, oldPrice As Currency


As you Tick code uses Range Target variable you will also need to pass to the code


Code:
Call Tick(Target, oldPrice, newPrice)

and your Update Tick Code

Code:
Sub Tick(ByVal Target As Range, ByVal newPrice As Currency, ByVal oldPrice As Currency)
    
    
    Cells(Target.Row, 4).Value = oldPrice
    
    If Cells(Target.Row, Target.Column).Value < Cells(Target.Row, 4).Value Then
        Cells(Target.Row, 2).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 10066431
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        Cells(Target.Row, 10).Value = Cells(Target.Row, 10).Value + 1
            
    Else
        Cells(Target.Row, 2).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 11919289
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        Cells(Target.Row, 9).Value = Cells(Target.Row, 9).Value + 1
        
    End If




End Sub


Hopefully, this will do what want.


dave
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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