How do I set a 'Worksheet_Change' event for part of a worksheet?

Phil Payne

Board Regular
Joined
May 17, 2013
Messages
131
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I am very much a VBA beginner and I cannot see a way to modify my code. I hope someone can help.

The first part of the code below checks for a change of Status in the Status column and on change fills the adjacent Cost cell with appropriate colour then enters current date into the adjacent Date cell. The range covered extends from column ‘R’ (first Status column), to column ‘EZ’ (last Status column). The second part records the date any part of a row was changed and who changed it.

This code operates well but it works from top to bottom of each column.

I want to use the top 14 rows of the worksheet for other calculations so I need to have this 'change event' restricted to Rows 15 down to last row only.

Can anyone advise how I adjust my code to achieve this?

Thanks.

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Count > 1 Then Exit Sub
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Dim rCell As Range
    Dim rCodes As Range
    Dim rRow As Range
    Dim vMatch

    Set rCodes = Range("E2:E12")

    If (Target.Column >= 18) And (Target.Column <= Range("EZ1").Column) And (Target.Column Mod 3 = 0) Then
        If Len(Target.Value) > 0 Then
            On Error Resume Next
            vMatch = Application.Match(Target.Value, rCodes, 0)
            If IsError(vMatch) Then
                MsgBox "Invalid code selected"
            Else
                With Target
                    .Offset(, 1).Interior.Color = rCodes.Cells(vMatch).Interior.Color
                    .Offset(0, 2).Value = Date
                End With
            End If
        End If
    End If
    
'CODE TO ENTER DATE ROW CHANGED IN CELL IN SAME ROW AT COLUMN [FG]
' The range covered extends from column ‘R’ (first Status column), to column ‘FA’ (last COST column).
' If adding columns adjust ranges accordingly!


   If Not Intersect(Target, Range("R:fb")) Is Nothing Then
      For Each rCell In Intersect(Target, Range("R:fb")).Cells
        If Target.Row > 1 Then Cells(Target.Row, "FG") = Now()
        If Target.Row > 1 Then Cells(Target.Row, "FH") = Environ("Username")
        ' THESE DESTINATION CELLS IN WORKSHEET MUST HAVE SECURITY PROTECTION OFF"
      Next rCell
   End If

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Maybe like this

Rich (BB code):
        Private Sub Worksheet_Change(ByVal Target As Range)

            If Target.Count > 1 Then Exit Sub
            If Target.Row < 15 Then Exit Sub
            With Application
                .ScreenUpdating = False
                .EnableEvents = False
            End With
 
Upvote 0
HelloVoG

Thanks very much - that works to allow me to use the top rows and is much simpler than the solution I just worked out, i.e. (.... If (Target.Column >= 18) And (Target.Column <= Range("EZ1").Column) And (Target.Column Mod 3 = 0) And (Target.Row >= 15) And (Target.Row <= 139) Then ......)

Unfortunately it does not stop the event from happening below the last row.
In my amended code I have had to specifically state the last row number of the range to ensure this happens however, as the table may 'grow' over time, I need to use something like xlDown but can't work out how!

Thanks very much for taking the time to assist.

Regards,
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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