Cut Cells Marked "Closed" and Paste to New Sheet

koolgeex

New Member
Joined
May 2, 2012
Messages
45
1 Workbook
2 Spreadsheets - "Complete Backlog" & "Closed Jobs"
If Column K in "Complete Backlog" says "Closed" then cut that row and paste it into the next available row of "Closed Jobs".

I have this code below but there is an error with it. It currently cuts all of the items marked closed but it doesn't paste all of them on the new sheet.
On my last go around, it cut 34 rows marked Closed and only pasted 20 rows into the new sheet. Suggestions?

Code:
Sub refresh()
Dim LR As Long, i As Long
With Sheets("Complete Backlog")
    LR = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    For i = 1 To LR
        If .Range("K" & i).Value = "Closed" Then .Range("A" & i).Resize(, 11).Cut Destination:=Sheets("Closed Jobs").Range("A" & Rows.Count).End(xlUp).Offset(1)
    Next i
    .Range("A2:K" & LR).SpecialCells(xlCellTypeBlanks).delete shift:=xlShiftUp
End With
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Your code looks ok. When it cut 30 and only pasted 20, was it the first 20, last 20 or ??? Do you have some event-driven code running in the background that might be interfering?
 
Upvote 0
Seems to be at random. No other codes running in the background. This it the only macro in the workbook and I use a button to run it. I did the analysis and here are my results:
31 rows were marked "Closed" in Spreadsheet1
All 31 rows were deleted.
20 rows were pasted.
These are all 31 row numbers that should have been deleted and pasted. The ones with an [x] next to it represent the 11 rows that were not pasted over. Hope this helps!

6 [x]
13
15
19
20 [x]
22
27
28
30 [x]
32
41
43
51 [x]
52 [x]
53 [x]
54 [x]
55 [x]
56 [x]
59
62
69 [x]
72 [x]
77
78
79
80
81
82
83
84
87
 
Upvote 0
Here's a version that requires no cut/paste. See if it handles all 31 rows.
Code:
Sub refresh2()
Dim LR As Long, i As Long
With Sheets("Complete Backlog")
    LR = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    For i = 1 To LR
        If .Range("K" & i).Value = "Closed" Then
        Sheets("Closed Jobs").Range("A" & _
            Rows.Count).End(xlUp).Offset(1).Value = .Range("A" & _
            i).Resize(, 11).Value
            .Range("A" & i).Resize(, 11).ClearContents
        End If
    Next i
    .Range("A2:K" & LR).SpecialCells(xlCellTypeBlanks).Delete shift:=xlShiftUp
End With
End Sub
 
Upvote 0
Doesn't look like it. I ran it and it cut the rows but only pasted column As content. Even though it only has column A, it only fills 20 rows so doesn't seem like it even pasted the full 31 rows.
 
Upvote 0
Doesn't look like it. I ran it and it cut the rows but only pasted column As content. Even though it only has column A, it only fills 20 rows so doesn't seem like it even pasted the full 31 rows.
Sorry, forgot to resize. Try this:
Code:
Sub refresh2()
Dim LR As Long, i As Long
With Sheets("Complete Backlog")
    LR = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    For i = 1 To LR
        If .Range("K" & i).Value = "Closed" Then
        Sheets("Closed Jobs").Range("A" & _
            Rows.Count).End(xlUp).Offset(1).Resize(1, 11).Value = .Range("A" & _
            i).Resize(, 11).Value
            .Range("A" & i).Resize(, 11).ClearContents
        End If
    Next i
    .Range("A2:K" & LR).SpecialCells(xlCellTypeBlanks).Delete shift:=xlShiftUp
End With
End Sub
 
Upvote 0
Thanks. Got an error msg. "Application-defined or object-defined error". Clicked Debug and it highlighted this section:
Code:
Sheets("Closed Jobs").Range("A" & _
            Rows.Count).End(xlUp).Offset(1).Resize(1, 11).Value = .Range("A" & _
            i).Resize(, 11).Value
 
Upvote 0
I suspect your issue may be case sensitivity in the If statement. Here's both versions revised to deal with that. I have tested the second version and it's working for me.
Code:
Sub refresh2()
Dim LR As Long, i As Long
With Sheets("Complete Backlog")
    LR = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    For i = 1 To LR
        If UCase(.Range("K" & i).Value) = UCase("closed") Then
        Sheets("Closed Jobs").Range("A" & _
            Rows.Count).End(xlUp).Offset(1).Resize(1, 11).Value = _
            .Range("A" & i).Resize(, 11).Value
            .Range("A" & i).Resize(, 11).ClearContents
        End If
    Next i
    .Range("A2:K" & LR).SpecialCells(xlCellTypeBlanks).Delete shift:=xlShiftUp
End With
End Sub

Code:
Sub refresh2()
Dim LR As Long, i As Long
With Sheets("Complete Backlog")
    LR = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    For i = 1 To LR
        If UCase(.Range("K" & i).Value) = UCase("closed") Then
        Sheets("Closed Jobs").Range("A" & _
            Rows.Count).End(xlUp).Offset(1).Resize(1, 11).Value = _
            .Range("A" & i).Resize(, 11).Value
            .Range("A" & i).Resize(, 11).ClearContents
        End If
    Next i
    .Range("A2:K" & LR).SpecialCells(xlCellTypeBlanks).Delete shift:=xlShiftUp
End With
End Sub
 
Upvote 0
Still getting the same error but I did notice it deleted 10 from the main sheet and pasted 7 in the "closed jobs" sheet. Still left 21 marked "close" on the backlog sheet. Man!
 
Upvote 0
Still getting the same error but I did notice it deleted 10 from the main sheet and pasted 7 in the "closed jobs" sheet. Still left 21 marked "close" on the backlog sheet. Man!
Is that a typo or are they really marked "close" not "closed"?

If it's a typo in your post, then I'm miffed. Both versions work fine for me.

One final thought would be to trim column K entries on the backlog sheet in the event there may be some leading space(s) before "Closed".
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

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