Run macro automatically when value changes

lunatu

Board Regular
Joined
Feb 5, 2021
Messages
77
Office Version
  1. 2010
Platform
  1. Windows
  2. Web
Hi,

Im trying to run a macro if value in column K changes into "Sent". This macro is working fine if i define only one cell (for example Set Target = Range("K15")) but not if I try to expand the range for entire column. What I am doing wrong with this macro?

Private Sub Worksheet_Change(ByVal Target As Range)
Set Target = Range("K:K")
If Target.Value = "Sent" Then
Call CopyCell
Call RemoveDuplicates
End If
End Sub

Thanks in advance!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Don't mess with "Target". It is a system-defined range, specifically the cell that is change that called the VBA code to run.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    
    Set rng = Intersect(Range("K:K"), Target)
    
    If rng Is Nothing Then Exit Sub
    
    For Each cell In rng
        If cell.Value = "Sent" Then
            Application.EnableEvents = False
            Call CopyCell
            Call RemoveDuplicates
            Application.EnableEvents = True
        End If
    Next cell
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

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