Issue with Duplicate Const & Dim Changed As Range

Jaikobi

New Member
Joined
Jul 31, 2019
Messages
1
Hi,
Trying to get these two instances to run together but not sure how to simplify.
Can provide any more information if needed.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Changed As Range
    
    Const YesCol As String = "L" '
    
    Set Changed = Intersect(Target, Columns(YesCol))
    If Not Changed Is Nothing Then
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        With Intersect(ActiveSheet.UsedRange, Columns(YesCol))
            .AutoFilter Field:=1, Criteria1:="=YES"
            With .Offset(1).EntireRow
                .Copy Destination:=Sheets("ARCHIVED") _
                    .Range("A" & Rows.Count).End(xlUp).Offset(1)
                .Delete
                End With
            .AutoFilter
            End With
              Application.EnableEvents = True
        Application.ScreenUpdating = True
    End If
    
    Const YesCol As String = "K" '
    
    Set Changed = Intersect(Target, Columns(YesCol))
    If Not Changed Is Nothing Then
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        With Intersect(ActiveSheet.UsedRange, Columns(YesCol))
            .AutoFilter Field:=1, Criteria1:="=NO"
            With .Offset(1).EntireRow
                .Copy Destination:=Sheets("DELIVERED") _
                    .Range("A" & Rows.Count).End(xlUp).Offset(1)
                .Delete
                End With
            .AutoFilter
            End With
              Application.EnableEvents = True
        Application.ScreenUpdating = True
    End If
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi & welcome to MrExcel
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
   If Not Intersect(Target, Columns("L")) Is Nothing Then
      Application.EnableEvents = False
      Application.ScreenUpdating = False
      With Intersect(Me.UsedRange, Columns("L"))
         .AutoFilter Field:=1, Criteria1:="=YES"
         With .Offset(1).EntireRow
            .Copy Destination:=Sheets("ARCHIVED") _
            .Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Delete
         End With
         .AutoFilter
      End With
      Application.EnableEvents = True
      Application.ScreenUpdating = True
   ElseIf Not Intersect(Target, Columns("K")) Is Nothing Then
      Application.EnableEvents = False
      Application.ScreenUpdating = False
      With Intersect(Me.UsedRange, Columns("K"))
         .AutoFilter Field:=1, Criteria1:="=NO"
         With .Offset(1).EntireRow
            .Copy Destination:=Sheets("DELIVERED") _
            .Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Delete
         End With
         .AutoFilter
      End With
      Application.EnableEvents = True
      Application.ScreenUpdating = True
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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