VBA moving rows

rolfet

New Member
Joined
Jul 22, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
hi all,

I am trying to get rows to move from "active" Sheet to "complete" sheet on the attached file once column "J" has been sent to "complete".

for the life of me i can not get it to work and as its a moving sheet thats updated daily i want it to move rows from one sheet to another so it only shows live jobs.

can anyone help me with the code as i tried using the below and cant get it to work?

Private Sub Worksheet_Change(ByVal Target As Range)

' Check to see only one cell updated
If Target.CountLarge > 1 Then Exit Sub

' Check to see if entry is made in column B after row 5 and is set to "Yes"
If Target.Column = 10 And Target.Row > 2 And Target.Value = "Complete" Then
Application.EnableEvents = False
' Copy columns B to I to complete sheet in next available row
Range(Cells(Target.Row, "B"), Cells(Target.Row, "K")).Copy Sheets("Complete").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
' Delete current row after copied
Rows(Target.Row).Delete
Application.EnableEvents = True
End If

End Sub


thanks for any help in advance
 

Attachments

  • ECU Tracker.png
    ECU Tracker.png
    86 KB · Views: 29

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Give this a try. Make sure the code is on the "Active" sheet.
1721664188183.png


Also, I assume you also have data in column A, correct? I did an example and if column A is Blank, then this code will need to be adjusted.

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim wsComplete As Worksheet
    Dim NextRow As Long

    If Target.CountLarge > 1 Then Exit Sub

    If Target.Column = 10 And Target.Row > 1 And Target.Value = "Complete" Then
        Application.EnableEvents = False

        Set wsComplete = ThisWorkbook.Sheets("Complete")
        NextRow = wsComplete.Cells(wsComplete.Rows.Count, "A").End(xlUp).Row + 1

        With Target.EntireRow
            .Range("A1:K1").Copy wsComplete.Cells(NextRow, "A")
            .Delete
        End With
        
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
Another way to try on a copy of your workbook.
Code:
Sub Or_Maybe_So()
If Not Application.CountIf(Columns(9), "Complete") = 0 Then
Application.ScreenUpdating = False
With Sheets("Sheet2").UsedRange
    .AutoFilter Field:=9, Criteria1:="Complete"
    With .Offset(1, 0).EntireRow.SpecialCells(xlCellTypeVisible)
        .Copy Sheets("Sheet3").Cells(Sheets("Sheet3").Rows.Count, 1).End(xlUp).Offset(1)
        .Delete (xlShiftUp)
    End With
    .AutoFilter
End With
Application.ScreenUpdating = True
End If
End Sub

The picture shows it to be column 9 so that's what I used.
Change as required.
 
Upvote 0
Give this a try. Make sure the code is on the "Active" sheet.
hi, this works by moving the data from the active sheet onto complete which is amazing. however, when i go onto complete sheet is pastes over the information on row 2 rather than adding each new data to a new line. if that makes sense? for eg when i sent to complete it goes into row 2 on complete sheet then when i set another to complete on the active sheet, rather than going to row 3 then 4 then 5 etc it re-write row 2.
 
Last edited by a moderator:
Upvote 0
hi, this works by moving the data from the active sheet onto complete which is amazing. however, when i go onto complete sheet is pastes over the information on row 2 rather than adding each new data to a new line. if that makes sense? for eg when i sent to complete it goes into row 2 on complete sheet then when i set another to complete on the active sheet, rather than going to row 3 then 4 then 5 etc it re-write row 2.
Hmmm, I think I understand. Let's try this, change this:
VBA Code:
NextRow = wsComplete.Cells(wsComplete.Rows.Count, "A").End(xlUp).Row + 1
to
VBA Code:
NextRow = wsComplete.Cells(wsComplete.Rows.Count, 1).End(xlUp).Row + 1
Let me know if that works.
 
Upvote 0
Hmmm, I think I understand. Let's try this, change this:
VBA Code:
NextRow = wsComplete.Cells(wsComplete.Rows.Count, "A").End(xlUp).Row + 1
to
VBA Code:
NextRow = wsComplete.Cells(wsComplete.Rows.Count, 1).End(xlUp).Row + 1
Let me know if that works.
i've change the code to suit but for some reason its stopped working altogether? to clarify i dont have any data in column A the data is between B to K starting from row 2 i have attached a clearer picture. Apologies!
 

Attachments

  • ECU Tracker.png
    ECU Tracker.png
    93.3 KB · Views: 25
Upvote 0
Put code in "Active" Sheet module.
Check references and spelling. Change where required.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lr As Long, rng As Range, c As Range
Dim wsA As Worksheet, wsC As Worksheet
Set wsA = Worksheets("Active")
Set wsC = Worksheets("Complete")
lr = wsA.Cells(wsA.Rows.Count, 10).End(xlUp).Row
Set rng = wsA.Range("J2:J" & lr)
If Target.Column = 10 Then
    For Each c In rng
        If c.Value = "Complete" Then c.Offset(, -8).Resize(, 10).Copy wsC.Cells(wsC.Rows.Count, 2).End(xlUp).Offset(1)
    Next c
End If
End Sub
If you want it pasted in Column A in Complete Sheet, Change the 2 to a 1 like so here.
Code:
wsC.Cells(wsC.Rows.Count, 1).End(xlUp).Offset(1)
 
Upvote 0
If the Cells need deleting, change this line
Code:
If c.Value = "Complete" Then c.Offset(, -8).Resize(, 10).Copy wsC.Cells(wsC.Rows.Count, 2).End(xlUp).Offset(1)
to these lines
Code:
If c.Value = "Complete" Then
With c.Offset(, -8).Resize(, 10)
.Copy wsC.Cells(wsC.Rows.Count, 2).End(xlUp).Offset(1)    '<---- or (wsC.Rows.Count, 1) to paste in Column A
.Delete Shift:=xlUp
End With
 
Upvote 0
i've change the code to suit but for some reason its stopped working altogether? to clarify i dont have any data in column A the data is between B to K starting from row 2 i have attached a clearer picture. Apologies!
Apologies for the delay - I had matters to attend to. I see this post still hasn't been solved, so I assume you're still having this issue.

I put together a test. I have the following on the Active sheet:
Test File.xlsm
ABCDEFGHIJKL
1DateTrailer numbe-LocationVendorDTC FitrLabelStatusInitiJob CompleAdditional Comments
28/7/202446701-20XPOLogistics GU 5DPNOR25Ye sYesSentJHB
318/07/202446715-20Great Bear DistributionABS02YesYesSentJH8
417/07/202445921-19FleetcareFLE02NoNoSentMH is waiting on Ant sending Pic & DTC
517/07/202447891-22xpoNOR25YesYesSentJHBawaiting ECU to be fit so SAF can collect old Unit
612/7/202446672-20Avonmouth Truck and TrailerAVOOIYesYesSentJHB
716/07/202446762-20Wincanton Bristol BS20 ONEAVOOINoNoWaiting on OTC & PicsDMCQWaiting on DTC& Pics.
88/7/202446410-20Naylors Barnsley S75 4ADHOEOIYesYesNeed to Replenish HoeysDMCQHoey's oingtofittheir own and we can re lenish
99/7/202447150-21M&S Bradford BD5 8LZJOS02YesYesGot from EMS DewsbuDMCQ
109/7/202446210-20Law Haydock WAII 9SZ81302Ye sYesSentDMCQ
118/7/202445871-19AS Commercials BarnsleyRMMOI/ASCOIYesYesSentDMCQ
128/7/202446521-20Aegus NewcastleJOS02Ye sYesSentDMCQ
139/7/202461068-20KCS sittingbourneKCS02Ye sYesSentDMCQ
1410/22/202446658-20Truckrite BristolTRU26YesYessentJHB
15
16
17
18
19
20
Active


I did 3 random tests within my table and selected 3 to type "Complete" on. They all 3 transferred over to the Complete sheet as follows:
Test File.xlsm
ABCDEFGHIJKL
1DateTrailer numbe-LocationVendorDTC FitrLabelStatusInitiJob CompleAdditional Comments
27/7/202431656-18TRUCKERS REST WS11 ISFDOC02YesYesBought from EMS as roadsideJHBComplete
318/07/202446209-20Law Haydock WAII 9SZBIB02YesYe sSentOMCQComplete
42/7/202446680-20MGreat Bear DistributionHIL06Ye sYessentMGComplete
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Complete


Here is the code that's insert onto the (Active) sheet:
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim wsComplete As Worksheet
    Dim tblComplete As ListObject
    Dim NextRow As ListRow
    
    If Target.CountLarge > 1 Then Exit Sub
    
    If Target.Column = 10 And Target.Row > 1 And Target.Value = "Complete" Then
        Application.EnableEvents = False
        
        Set wsComplete = ThisWorkbook.Sheets("Complete")
        Set tblComplete = wsComplete.ListObjects(1)
        
        Set NextRow = tblComplete.ListRows.Add
        
        With Target.EntireRow
            tblComplete.ListRows(NextRow.Index).Range.Columns(1).Resize(, 10).Value = .Range("B1:K1").Value
            .Delete
        End With
        
        Application.EnableEvents = True
    End If
End Sub

Hopefully 3rd times the charm!
 
Upvote 0
Apologies for the delay - I had matters to attend to. I see this post still hasn't been solved, so I assume you're still having this issue.

I put together a test. I have the following on the Active sheet:
Test File.xlsm
ABCDEFGHIJKL
1DateTrailer numbe-LocationVendorDTC FitrLabelStatusInitiJob CompleAdditional Comments
28/7/202446701-20XPOLogistics GU 5DPNOR25Ye sYesSentJHB
318/07/202446715-20Great Bear DistributionABS02YesYesSentJH8
417/07/202445921-19FleetcareFLE02NoNoSentMH is waiting on Ant sending Pic & DTC
517/07/202447891-22xpoNOR25YesYesSentJHBawaiting ECU to be fit so SAF can collect old Unit
612/7/202446672-20Avonmouth Truck and TrailerAVOOIYesYesSentJHB
716/07/202446762-20Wincanton Bristol BS20 ONEAVOOINoNoWaiting on OTC & PicsDMCQWaiting on DTC& Pics.
88/7/202446410-20Naylors Barnsley S75 4ADHOEOIYesYesNeed to Replenish HoeysDMCQHoey's oingtofittheir own and we can re lenish
99/7/202447150-21M&S Bradford BD5 8LZJOS02YesYesGot from EMS DewsbuDMCQ
109/7/202446210-20Law Haydock WAII 9SZ81302Ye sYesSentDMCQ
118/7/202445871-19AS Commercials BarnsleyRMMOI/ASCOIYesYesSentDMCQ
128/7/202446521-20Aegus NewcastleJOS02Ye sYesSentDMCQ
139/7/202461068-20KCS sittingbourneKCS02Ye sYesSentDMCQ
1410/22/202446658-20Truckrite BristolTRU26YesYessentJHB
15
16
17
18
19
20
Active


I did 3 random tests within my table and selected 3 to type "Complete" on. They all 3 transferred over to the Complete sheet as follows:
Test File.xlsm
ABCDEFGHIJKL
1DateTrailer numbe-LocationVendorDTC FitrLabelStatusInitiJob CompleAdditional Comments
27/7/202431656-18TRUCKERS REST WS11 ISFDOC02YesYesBought from EMS as roadsideJHBComplete
318/07/202446209-20Law Haydock WAII 9SZBIB02YesYe sSentOMCQComplete
42/7/202446680-20MGreat Bear DistributionHIL06Ye sYessentMGComplete
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Complete


Here is the code that's insert onto the (Active) sheet:
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim wsComplete As Worksheet
    Dim tblComplete As ListObject
    Dim NextRow As ListRow
   
    If Target.CountLarge > 1 Then Exit Sub
   
    If Target.Column = 10 And Target.Row > 1 And Target.Value = "Complete" Then
        Application.EnableEvents = False
       
        Set wsComplete = ThisWorkbook.Sheets("Complete")
        Set tblComplete = wsComplete.ListObjects(1)
       
        Set NextRow = tblComplete.ListRows.Add
       
        With Target.EntireRow
            tblComplete.ListRows(NextRow.Index).Range.Columns(1).Resize(, 10).Value = .Range("B1:K1").Value
            .Delete
        End With
       
        Application.EnableEvents = True
    End If
End Sub

Hopefully 3rd times the charm!
thank you for taking the time to look at this for me. For some reason the code wont work for me. I have opened a new workbook and laid it out from B to K as above and saved it as macro enabled, copied and pasted the above code however it still doesn't work for me? is there a way you can send me your test file and i can save and re-use that one? sorry for being a pain!!
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,145
Members
452,615
Latest member
bogeys2birdies

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