Seems easy but losing my mind... Combine Worksheet_Change

Drivium

New Member
Joined
Nov 4, 2011
Messages
28
I have these two pieces of code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Value Like "Moved to last planner" Then

Sheets("Last Planner").Range("A" & Rows.Count).Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value
Else
If Target.Value <> "Moved to last planner" Then
End If
End If
End Sub
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
For i = 1 To 30
If Target.Value Like "Not Implemented" Then

If Sheets("Last Planner").Range("A" & i) = Target.Offset(0, 2 - Target.Column()).Value Then
Sheets("Last Planner").Range("A" & i) = ""
Else

End If
End If
Next i
End Sub

They both work great independently, but I need to somehow merge them to fit together under Worksheet_Change as 1 piece of code. Ive tried everything, but Im getting errors like "End if without block if", "object not defined", "Next without for"... TIA
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi,

You could have a first try with following :

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
    If Target.Count > 1 Then Exit Sub
    If Target.Column > 1 Then Exit Sub

    If Target.Value Like "Moved to last planner" Then
        Sheets("Last Planner").Range("A" & Rows.Count).Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value
    Else
        If Target.Value Like "Not Implemented" Then
        For i = 1 To 30
         If Sheets("Last Planner").Range("A" & i) = Target.Offset(0, 2 - Target.Column()).Value Then Sheets("Last Planner").Range("A" & i) = ""
        Next i
        End If
    End If
End Sub

HTH
 
Last edited:
Upvote 0
I would say: remove the last line of the first block of code and the first line of the last block of code, then stitch what's left of the two blocks together.

Another way of putting it: remove the first and last lines from the second block of code and insert what's left before the last line of the first block of code.
 
Upvote 0
Hi,

You could have a first try with following :

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
    If Target.Count > 1 Then Exit Sub
    If Target.Column > 1 Then Exit Sub

    If Target.Value Like "Moved to last planner" Then
        Sheets("Last Planner").Range("A" & Rows.Count).Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value
    Else
        If Target.Value Like "Not Implemented" Then
        For i = 1 To 30
         If Sheets("Last Planner").Range("A" & i) = Target.Offset(0, 2 - Target.Column()).Value Then Sheets("Last Planner").Range("A" & i) = ""
        Next i
        End If
    End If
End Sub

HTH

I tried this, it doesn't error but it also seems to do nothing. In the column, there are a several cells where "moved to last planner" or "not implemented" may exist. In other words there are a range of cells in the column that might meet the criteria...

Maybe something in this stopping it from moving forward?
Code:
    If Target.Count > 1 Then Exit Sub
    If Target.Column > 1 Then Exit Sub
 
Upvote 0
I still can't get it... I even tried leaving the two pieces of code in tact and just creating another to call each of those if the change meets the criteria, but I get "Argument not optional" error:

Code:
Private Sub Moved_to_last_planner(ByVal Target As Range)

If Target.Value Like "Moved to last planner" Then

Sheets("Last Planner").Range("A" & Rows.Count).Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value
Else
If Target.Value <> "Moved to last planner" Then
End If
End If
End Sub

Private Sub Not_Implemented(ByVal Target As Range)
For i = 1 To 30
If Target.Value Like "Not Implemented" Then

If Sheets("Last Planner").Range("A" & i) = Target.Offset(0, 2 - Target.Column()).Value Then
Sheets("Last Planner").Range("A" & i) = ""
Else

End If
End If
Next i
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Value Like "Moved to last planner" Then
    Call Moved_to_last_planner
 Else
     If Target.Value Like "Not Implemented" Then
    Call Not_Implemented
    End If
              End Sub
 
Upvote 0
Ruddles, I absolutely did, and thank you for it. As far as I could tell, I resulting code looked like it matched your suggestion, but it still generated errors.
 
Upvote 0
Ruddles - if I'm understanding correctly, this would be the resulting code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Value Like "Moved to last planner" Then

Sheets("Last Planner").Range("A" & Rows.Count).Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value
Else
If Target.Value <> "Moved to last planner" Then
End If
For i = 1 To 30
If Target.Value Like "Not Implemented" Then

If Sheets("Last Planner").Range("A" & i) = Target.Offset(0, 2 - Target.Column()).Value Then
Sheets("Last Planner").Range("A" & i) = ""
Else

End If
End If
Next i
End If
End Sub

When "Not Implemented" is selected, it works as it should, but when "Moved to last planner" is selected, it generates this error: Object variable or with block variable not set. It also highlights this piece of code:
Code:
Sheets("Last Planner").Range("A" & Rows.Count).Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value

The fact that at least a piece of this combo works is promising though!
 
Upvote 0
Perhaps something like this.
Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim I As Long

    Select Case Target.Value
        Case "Moved to last planner"

            Sheets("Last Planner").Range("A" & Rows.Count).Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value

        Case "Not Implemented"

            For I = 1 To 30

                If Sheets("Last Planner").Range("A" & I) = Target.Offset(0, 2 - Target.Column()).Value Then
                    Sheets("Last Planner").Range("A" & I) = ""
                End If
            Next I
    End Select
    
End Sub
 
Upvote 0
This case statement from Norie, and probably James006 statement WOULD have worked... I just realized a few attempts ago, I changed:

(GOOD CODE)
Code:
Sheets("Last Planner").Range("A:A").Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value

To (BAD CODE)
Code:
Sheets("Last Planner").Range("A" & Rows.Count).Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value

I have it working now with this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim I As Long

    Select Case Target.Value
        Case "Moved to last planner"

            Sheets("Last Planner").Range("A:A").Find("").End(xlUp).Offset(1, 0) = Target.Offset(0, 2 - Target.Column()).Value

        Case "Not Implemented"

            For I = 1 To 30

                If Sheets("Last Planner").Range("A" & I) = Target.Offset(0, 2 - Target.Column()).Value Then
                    Sheets("Last Planner").Range("A" & I) = ""
                End If
            Next I
    End Select
    
End Sub

Thank you so much to all who contributed. Solved!
 
Upvote 0

Forum statistics

Threads
1,214,883
Messages
6,122,077
Members
449,064
Latest member
MattDRT

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