Help Combining 2 Private Sub Worksheet_Change(ByVal Target As Range) Codes

brett1again

New Member
Joined
Jun 2, 2022
Messages
10
Office Version
  1. 365
Platform
  1. MacOS
Hello,
I'm trying to combine 2 worksheet_change codes and I can't seem to get it figured out. I'd really appreciate some help.

Here are the 2 codes separately:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Not Intersect(Target, Range("U:U")) Is Nothing Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("AD:AD")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Dim Lastrow As Long
Lastrow = Sheets("COMPLETED").Cells(Rows.Count, "AD").End(xlUp).Row + 1

If Target.Value = "CLOSE" Then
Rows(Target.Row).Copy Destination:=Sheets("COMPLETED").Rows(Lastrow)
Rows(Target.Row).Delete
End If
End If
End Sub

Hope it's not to much to ask but once I get these combined I was also needing to add a code that piggybacks the move row/delete row code above. The code above moves the row from sheets"Tracker" to sheets"completed" when "close" is selected and then deletes it from sheets"tracker". I'd like to be able to move the row back to its original position if it was closed in error by selecting "return" in the same AD column. I've tried to create the code but I'm a definite novice when it comes to VBA and thought I'd ask for some help. Thanks in advance for the help!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi and welcome to MrExcel!

I'd like to be able to move the row back to its original position if it was closed in error by selecting "return" in the same AD column.
You could not select "return" from the deleted record, simply because it no longer exists.
You should put the code in the "COMPLETED" sheet, there select the record that you want to return to the "tracker" sheet.

If you want it back in the same position, then when you copy it from the "tracker" sheet to the "COMPLETED" sheet, you must put in some column of that record the original row number, that way you can return it to its original row.

---------------
I'm trying to combine 2 worksheet_change codes

I give you the combined codes:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Oldvalue As String
  Dim Newvalue As String
  Dim xRng As Range
  Dim Lastrow As Long
  
  If Target.Count > 1 Then Exit Sub
  
  On Error GoTo Exitsub
  Set xRng = Range("U:U").SpecialCells(xlCellTypeAllValidation)
  On Error GoTo 0
  
  If Not Application.Intersect(Target, xRng) Is Nothing Then
  
    Application.EnableEvents = False
    
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
    Target.Value = Newvalue
    
    If Oldvalue <> "" Then
      If Newvalue <> "" Then
      
        If Oldvalue = Newvalue Or _
          InStr(1, Oldvalue, ", " & Newvalue) Or _
          InStr(1, Oldvalue, Newvalue & ",") Then
          Target.Value = Oldvalue
        Else
          Target.Value = Oldvalue & ", " & Newvalue
        End If
        
      End If
    End If
        
    Application.EnableEvents = True
    
  End If
  
  '********Delete record
  If Not Intersect(Target, Range("AD:AD")) Is Nothing Then
    If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
    Lastrow = Sheets("COMPLETED").Cells(Rows.Count, "AD").End(xlUp).Row + 1
    
    If Target.Value = "CLOSE" Then
      Rows(Target.Row).Copy Destination:=Sheets("COMPLETED").Rows(Lastrow)
      Rows(Target.Row).Delete
    End If
  End If

Exitsub:
End Sub
 
Upvote 0
Solution
Hi and welcome to MrExcel!


You could not select "return" from the deleted record, simply because it no longer exists.
You should put the code in the "COMPLETED" sheet, there select the record that you want to return to the "tracker" sheet.

If you want it back in the same position, then when you copy it from the "tracker" sheet to the "COMPLETED" sheet, you must put in some column of that record the original row number, that way you can return it to its original row.

---------------


I give you the combined codes:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Oldvalue As String
  Dim Newvalue As String
  Dim xRng As Range
  Dim Lastrow As Long
 
  If Target.Count > 1 Then Exit Sub
 
  On Error GoTo Exitsub
  Set xRng = Range("U:U").SpecialCells(xlCellTypeAllValidation)
  On Error GoTo 0
 
  If Not Application.Intersect(Target, xRng) Is Nothing Then
 
    Application.EnableEvents = False
   
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
    Target.Value = Newvalue
   
    If Oldvalue <> "" Then
      If Newvalue <> "" Then
     
        If Oldvalue = Newvalue Or _
          InStr(1, Oldvalue, ", " & Newvalue) Or _
          InStr(1, Oldvalue, Newvalue & ",") Then
          Target.Value = Oldvalue
        Else
          Target.Value = Oldvalue & ", " & Newvalue
        End If
       
      End If
    End If
       
    Application.EnableEvents = True
   
  End If
 
  '********Delete record
  If Not Intersect(Target, Range("AD:AD")) Is Nothing Then
    If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
    Lastrow = Sheets("COMPLETED").Cells(Rows.Count, "AD").End(xlUp).Row + 1
   
    If Target.Value = "CLOSE" Then
      Rows(Target.Row).Copy Destination:=Sheets("COMPLETED").Rows(Lastrow)
      Rows(Target.Row).Delete
    End If
  End If

Exitsub:
End Sub
That worked great! Really appreciate your help...thanks
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Hope you don't mind but I discovered a error and thought I'd ask for your help again. The code you gave me works great. I went ahead and added a new code to the "completed" sheet that will return the row back to the top row in the "tracker" sheet (actually the 2nd row because the 1st row is frozen), moving everything else done one to make room. It works fine but I have a sumproduct formula in one of the columns that's erroring after the row is returned. I'm not sure if it's the formula or the code? Can you give me your thoughts?

Here is the formula:
Excel Formula:
=SUMPRODUCT(ISNUMBER(SEARCH('Formatting Lists'!$A$1:$A$11,'Burial Tracker'!$U2))*'Formatting Lists'!$B$1:$B$11)+IF($V2>250,($V2-250)*1.5,0)+($W2*8.5)

Here is the code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("AD:AD")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Dim Lastrow As Long
Lastrow = Sheets("BURIAL TRACKER").Cells(Rows.Count, "AD").End(xlUp).Row + 1

If Target.Value = "RETURN" Then
Rows(Target.Row).Copy Destination:=Sheets("BURIAL TRACKER").Rows(Lastrow)
Rows(Target.Row).Delete
End If
End If
End Sub
 
Upvote 0
This does not correspond to the original requirement. You must create a new thread.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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