Duplicate Subroutine names for moving rows to different sheets and workbook

PerryK

New Member
Joined
May 8, 2018
Messages
27
Hello everyone, I am brand new to coding, and I am trying to use the code to move rows to different sheets and to move completed rows to a different work and I am having trouble that the Sub Worksheet_Change is being seen as ambiguous name and doesn't work when I try to change the name to something like Worksheet_ChangeCOMPLETE or WorkSheet_Change3, also rngDest has the same issue in all 3 codes, below is the codes that I am trying to use. What my plan is that I want completed orders (rows) to move to a new workbook in which I have named "COMPLETED" when a command button is pushed which triggers a Macro to insert the word "COMPLETE" in column 13 (M). This new workbook was formerly my sheet 2 but I made it a new workbook and saved it as COMPLETED.xlxs I also need rows to move to sheet 3 when "PARTIAL HOLD" inserted in column 13 via a different command button and then returned to sheet one when the command button on sheet 3 "RESUME" is clicked. All workbooks and worksheets have all the same columns and spacing, I just can't get the codes to work when I rename them. The first set of codes I am posting are for moving rows from sheet 1 to sheet 3 when the command button is pressed, followed by the code to move rows to the new workbook these codes are in Sheet 1 under VBA project, not a module. The third is on sheet 3 to move rows back to sheet 1 once HOLD is complete. Thank you in advance for your help.


SHEET 1
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDest As Range
Set rngDest = Sheet3.Range("A5:R5")
If Not Intersect(Target, Sheet1.Range("M5:M290")) Is Nothing Then
If UCase(Target) = "PARTIAL HOLD" Then
Application.EnableEvents = False
Target.EntireRow.Select
Selection.Cut
rngDest.Insert shift:=xlDown
Selection.Delete
Application.EnableEvents = True
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Dim destWbk As String
Dim wbk As Workbook
Dim rngDest As Range
destWbk = ThisWorkbook.names("COMPLETED.xlsx").RefersTo
destWbk = Replace(destWbk, "=" & Chr(34), "")
destWbk = Replace(destWbk, Chr(34), "")
Set wbk = Application.Workbooks(destWbk)
Set rngDest = wbk.names("A1:S1").RefersToRange
If Not Intersect(Target, Sheet1.Range("COMPLETE")) Is Nothing Then
If UCase(Target) = "COMPLETED" Then
Application.EnableEvents = False
Target.EntireRow.Select
Selection.Cut
rngDest.Insert shift:=xlDown
Selection.Delete

Application.EnableEvents = True
End If
End If
End Sub

SHEET 3

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDest As Range
Set rngDest = Sheet1.Range("A5:S5")
If Not Intersect(Target, Sheet3.Range("M5:M290")) Is Nothing Then
If UCase(Target) = "IN PROGRESS" Then
Application.EnableEvents = False
Target.EntireRow.Select
Selection.Cut
rngDest.Insert shift:=xlDown
Selection.Delete
Application.EnableEvents = True
End If
End If
End Sub
 
Last edited by a moderator:
JLGWhiz first off thank you very much for your patience and all the time and help you have provided me so far. The code is on sheet 1 and the row that I am trying to move is on sheet 3 and even after implementing your amendment to line 16 it is still not working. Is there code I need to put on sheet 3 to enable this move to happen or am I trying to do something that is impossible? Thank you again.

What is the sheet name in which your code resides? Is it Sheet1 or Sheet3? Or is it a different sheet altogether? The problem is that the target variable only applies to the sheet holding the code, and your code as posted is trying to use it for a different sheet. But to clear it up, I need to understand which sheet the code is actually in, then maybe I can get the syntax straightened out. I also need to confirm that the row number will always be the some on both sheets as the data is transferred back and forth.
 
Last edited:
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
In my test workbook I have left the names as Sheet 1, Sheet 2, and Sheet 3. In my actual workbook the names are Active Work Orders, Completed Work Orders, Partial Work Orders respectively. The code is residing in sheet 1. The row number for pasting in both sheet 1 and sheet 3 will be row 5. Specifically A5:P5. I was told I could only have one Worksheet_Change per workbook this was the code that was given to me to try. That is also why the target is on sheet 3 but the code is on sheet 1.

Thank you very much.
 
Last edited:
Upvote 0
I am rewriting the code, but I do not understand what is being attempted with this part.


Code:
 destWbk = ThisWorkbook.Names("COMPLETED.xlsx").RefersTo
 destWbk = Replace(destWbk,"="& Chr(34),"")
 destWbk = Replace(destWbk, Chr(34),"")
Set wbk = Application.Workbooks(destWbk)
Set rngDest = wbk.Names("A1:S1").RefersToRange

It looks like you are trying to set up a named range, but the syntax does not appear to be right and then it is not logical within the code for what you have stated as your objective. Any comments?
 
Upvote 0
I am rewriting the code, but I do not understand what is being attempted with this part.


Code:
 destWbk = ThisWorkbook.Names("COMPLETED.xlsx").RefersTo
 destWbk = Replace(destWbk,"="& Chr(34),"")
 destWbk = Replace(destWbk, Chr(34),"")
Set wbk = Application.Workbooks(destWbk)
Set rngDest = wbk.Names("A1:S1").RefersToRange

It looks like you are trying to set up a named range, but the syntax does not appear to be right and then it is not logical within the code for what you have stated as your objective. Any comments?

My apologies I thought I had removed that. That is leftover code from when I was trying to move completed orders to a new workbook. I have since decided to only move it to sheet 2.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDest As Range, rngDest2 As Range, rngDest3 As Range
     If UCase(Target.Value) = "PARTIAL HOLD" Then
        Set rngDest = Sheet3.Range("A5:P5")
        If Not Intersect(Target, Sheet1.Range("M5:M290")) Is Nothing Then
            Application.EnableEvents = False
            Target.EntireRow.Cut
            rngDest.Insert Shift:=xlDown
            Target.EntireRow.Delete
            Application.EnableEvents = True
        End If
    ElseIf UCase(Target.Value) = "PROGRESSING" Then
        Set rngDest3 = Sheet1.Range("A5:P5")
        If Not Intersect(Sheet3.Cells(Target.Row), Sheet3.Range("M5:M290")) Is Nothing Then
            Application.EnableEvents = False
            Target.EntireRow.Cut
            rngDest3.Insert Shift:=xlDown
            Target.EntireRow.Delete
            Application.EnableEvents = True
        End If
    ElseIf UCase(Target.Value) = "COMPLETE" Then
        Set rngDest2 = Sheet2.Range("A5:P5")
        If Not Intersect(Target, Sheet1.Range("M5:M290")) Is Nothing Then
             Application.EnableEvents = False
             Target.EntireRow.Cut
             rngDest2.Insert Shift:=xlDown
             Target.EntireRow.Delete
             Application.EnableEvents = True
        End If
    End If
End Sub


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   If Target.Column = 11 Then
      Cancel = True
      Target.Offset(, 2).Value = "IN PROGRESS"
      Target.Offset(, 4).Value = Time
   ElseIf Target.Column = 12 Then
      Cancel = True
      Target.Offset(, 1).Value = "COMPLETE"
      Target.Offset(, 4).Value = Time
   ElseIf Target.Column = 14 Then
      Cancel = True
      Target.Offset(, -1).Value = "PARTIAL HOLD"
   End If
End Sub

Here is the corrected code that I am working with, that is only moving rows between sheets not to a new workbook. Thank you and again I apologize for the excess code.
 
Upvote 0
This is what I have so far. This assuemes that the user is is entering status in column M of sheet 1 and as the status is entered the code will run.


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Sheet1.Range("M5:M290"))IsNothingThen
    Application.EnableEvents =False
    Dim rngDest As Range
        If UCase(Target.Value)="PARTIAL HOLD"Then            
            Set rngDest = Sheet3.Range("A5")            
            Target.EntireRow.Cut
            rngDest.EntireRow.Insert Shift:=xlDown
            Target.Row.Delete         
        ElseIf UCase(Target.Value)="IN PROGRESS"Then 
     Set rngDest = Sheet1.Range("A5")
            Target.EntireRow.Cut
            rngDest.EntireRow.Insert Shift:=xlDown
            Target.EntireRow.Delete            
        EndIf
    Application.EnableEvents =True
    End If
End Sub

The problem I am having is understanding the source and destination sheets. As I now understand it, all of your worksheets are in the same workbook and you have three sheets, Active, Compoleted and Partial. You only want the macro to run from sheet1 when changes occur in column M and whichever row that change is on will be cut and inserted into the appropriate sheet row 5 based on the entry in column M. The part that confuses me is trying to move the row back, because if a couple of more changes were made that inserted rows on a sheet, the row moves downward and there is no way to identify where it is now located.
 
Upvote 0
To try to help alleviate the confusion. Yes you are 100% correct that all 3 sheets are in one workbook. Active, Complete, and Partial. I will try to explain what the overall vision is and see if that helps you understand what I am trying to do.

Sheet 1 will be the origin where all orders for the day will be uploaded. That is where everything will begin. Row 5 is the first row in which orders are placed after the headers and date and time etc. The goal is that when working on an order all anyone has to do is click on the TIME IN (column K) cell. This will place the words "IN PROGRESS" in column M and the time in column O. If nothing goes wrong and the truck is loaded and takes off, the person will then click on TIME OUT (column L) cell. This will place the word "COMPLETE" in column M and the time in column P as well as move the row to Sheet 2. If there is a problem either with the truck or the order, the worker can click the HOLD (column N) cell. This will move the row to sheet 3. Sheet 3 is used to simply identify which trucks are held up and why. Once the issue is fixed I want the worker to then click on the cell in column N and this will place the word "PROGRESSING" into column M and move the row back to sheet 1 where it'll be placed at the top of the list of active orders as it will have been at a dock the longest making it a priority to complete.

I had attempted to do this using separate worksheet change routines, but I was getting an ambiguous name error. This led to where I am at now with all three procedures in one subroutine that resides on sheet 1, but has a target on sheet 3.
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target = "" Then Exit Sub
If Not Intersect(Target, Range("M:M") Is Nothing Then
 If UCase(Target.Value) = "COMPLETE" Then
  Target.EntireRow.Cut
  Sheets("Sheet2").Range("A5").EntireRow.Insert xlShiftDown
 ElseIf UCase(Target.Value) = "PARTIAL HOLD") Then
  Target.EntireRow.Cut
  Sheets("Sheet3").Range("A5").EntireRow.Insert xlShiftDown
 ElseIf UCase(Target.Value) = "PROGRESSING") tHEN
  Sheets("Sheet3").Range("A5").Cut '[COLOR=#ff0000]This is the one that does not follow the logic[/COLOR]
  Sheets("Sheet1").Range("A5").EntireRow.Insert xlShiftDown
 End If
End If
End Sub

two of them are easy and logical. It is moving the row back from sheet 3 that I am hung up on, because there is not guarantee that row 5 will contain the same row that the the "PROGRESSING" status is entered on in sheet 1. If there is a column that contains a unique identification for that truck, then I could include a 'Find' statement that would locate the specific row in sheet 3 and return it.
 
Last edited:
Upvote 0
Every order (row) will have its own unique order number in column B if that helps you think of a plan.

My thought was that it would move all of the information with it from sheet 1 to sheet 3. If it did that wouldn't that eliminate the ability to find the order number on sheet 1? I have all 3 sheets set up the exact same with the number of columns and the column headers. I hope this screen shot helps you see what I'm trying to do and helps give you ideas. All information on this pic is fake and typed in column Q is a sum formula of columns O and P, but that has nothing to do with the issue at hand.

 
Upvote 0
The screen shot didn't take, but it is unnecessary. I now understand what you are doing. You are correct that if you remove the data from sheet 1 to sheet 3 there would be nothing left on sheet 1 to reference. So on which sheet is the status being entered that triggers the macro. If it is on sheet 3, then you would have to use a separate macro in sheet 3 to move the row back to sheet 1, and that is no problem. The rule is, that you cannot have more than one event macro of the same name in a single parent Each sheet is a separate parent with its own code module, so you can have a Worksheet_Change macro in Sheet1 and in Sheet3. I will post your code in just a few moments.
 
Upvote 0
I had some typos in the last code I posted anyhow. The code below should do what you want. Give it a try and let me know. Note that one goes in sheet1 and the other in sheet3

This one goes into sheet 1 code module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target = "" Then Exit Sub
    If Not Intersect(Target, Range("M:M")) Is Nothing Then
        If UCase(Target.Value) = "COMPLETE" Then
            Target.EntireRow.Cut
            Sheets("Sheet2").Range("A5").EntireRow.Insert xlShiftDown
            Target.EntireRow.Delete
        ElseIf UCase(Target.Value) = "PARTIAL HOLD" Then
            Target.EntireRow.Cut
            Sheets("Sheet3").Range("A5").EntireRow.Insert xlShiftDown
            Target.EntireRow.Delete
        End If
    End If
End Sub
This one goes into sheet 3 Code module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target = "" Then Exit Sub
If Not Intersect(Target, Range("M:M")) Is Nothing Then
    If UCase(Target.Value) = "PROGRESSING" Then
        Target.EntireRow.Cut
        Sheets("Sheet1").Range("A5").EntireRow.Insert
        Target.EntireRow.Delete
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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