Running Macro after data has been entered into a cell

brhyne

New Member
Joined
Jun 2, 2009
Messages
8
Hello!

I have been trying to get the below code to run after I have entered a date in column B and exit the Cell. The code works perfectly if I go out of the cell in column B and then go back into it. After reentering B the code runs flawlessly. I have searched relentlessly for a solution with no resolve. if anybody has any Ideas I would be forever grateful.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim KeyCells As Range

Set KeyCells = Range("b2:b1048576")

If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then

Select Case ActiveCell(1, 1).Value

Case Is = ""
ActiveCell(1, 22).Value = ""
ActiveCell(1, 0).Value = ""


Case Is < Date
ActiveCell(1, 22).Value = Date
ActiveCell(1, 0).Value = Date

Case Is >= Date
ActiveCell(1, 22).Value = ActiveCell(1, 1).Value
ActiveCell(1, 0).Value = Date

End Select

End If

End Sub


I'm new to this forum so I hope I'm posting to the right area.

Thanks in advance for your help!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try using:
"Private Sub Worksheet_Change(ByVal Target As Excel.Range)"
instead of:
"Private Sub Worksheet_SelectionChange(ByVal Target As Range)"
 
Upvote 0
Sorry,
If you use Worksheet_Change you need to use "Target" in your code, as "Activecell" is normally one cell lower after hitting Enter.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("b2:b1048576")
    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
        Select Case Target.Value
            Case Is = ""
            Target(, 22).Value = ""
            Target(, 0).Value = ""
            
            Case Is < Date
            Target(, 22).Value = Date
            Target(, 0).Value = Date
            
            Case Is >= Date
            Target(, 22).Value = Target(, 2).Value
            Target(, 0).Value = Date
        End Select
    End If
End Sub
 
Upvote 0
Thanks John!, I was able to resolve the issue using the following code.

Code:
Option Explicit

Public Sub Worksheet_Change(ByVal Target As Excel.Range)
   Sheets(1).Protect Password:="Start123", _
          UserInterFaceOnly:=True, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
          AllowSorting:=True, AllowFiltering:=True


If Not Application.Intersect(Range("B2:B1048576"), Target) Is Nothing Then
Application.EnableEvents = False



Select Case Target(1, 1).Value

Case Is = ""
Target(1, 22).Value = ""
Target(1, 0).Value = ""

Case Is < Date
Target(1, 22).Value = Date
Target(1, 0).Value = Date

Case Is >= Date
Target(1, 22).Value = Target(1, 1).Value
Target(1, 0).Value = Date

End Select
Application.EnableEvents = True

End If
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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