cut/copy and paste row based on cell value not being equal to specific text

mlan1604

New Member
Joined
Jun 4, 2014
Messages
11
Hello
I'm moving completed tasks from one sheet to another and want to be able to move a task back into my worklist sheet.
I have a vba code in Excel for cutting and pasting rows from one sheet to another based on cell value="completed". Now I want to be able to reverse fromthe completed worksheet, by cutting and pasting rows not equal to the specific text string.
I thought, that I could just change "=" to "<>" in my code line [If sourceSheet.Cells(i, "C").Value = "Completed" Then], but then I get the error mesage, that my clipboard is used by another application. I have a list of choices (not startet; ongoing; delayed; completed), so I can't just pick another specific text string.

My code:
Sub MoveRowsToCompleted()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Long
Dim i As Long

' Set the source and target sheets
Set sourceSheet = ThisWorkbook.Worksheets("Worklist")
Set targetSheet = ThisWorkbook.Worksheets("Completed")

' Find the last row in the source sheet
lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "B").End(xlUp).Row

' Loop through each row in the source sheet
For i = 3 To lastRow
' Check if cell in column C contains "Completed"
If sourceSheet.Cells(i, "C").Value = "Completed" Then
' Copy the entire row to the target sheet
sourceSheet.Rows(i).Copy Destination:=targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Offset(1)
' Delete the row from the source sheet
sourceSheet.Rows(i).Delete
' Decrement the loop counter as the rows are shifting up
i = i - 1
' Update the last row value
lastRow = lastRow - 1
End If
Next i
End Sub

Sub MoveRowsToWorklist()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Long
Dim i As Long

' Set the source and target sheets
Set sourceSheet = ThisWorkbook.Worksheets("Completed")
Set targetSheet = ThisWorkbook.Worksheets("Worklist")

' Find the last row in the source sheet
lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "B").End(xlUp).Row

' Loop through each row in the source sheet
For i = 3 To lastRow
' Check if cell in column C contains "Completed"
If sourceSheet.Cells(i, "C").Value <> "Completed" Then
' Copy the entire row to the target sheet
sourceSheet.Rows(i).Copy Destination:=targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Offset(1)
' Delete the row from the source sheet
sourceSheet.Rows(i).Delete
' Decrement the loop counter as the rows are shifting up
i = i - 1
' Update the last row value
lastRow = lastRow - 1
End If
Next i
End Sub

Can someone help me solve the problem?

Thanks
MLAN1604
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I made new macros with another approach to move the records from one sheet to another:

VBA Code:
Sub MoveRowsToCompleted_v1()
  Application.ScreenUpdating = False
  With Sheets("Worklist")
    .Range("A2", .Range("C" & Rows.Count).End(3)).AutoFilter 3, "Completed"
    .AutoFilter.Range.Offset(1).EntireRow.Copy Sheets("Completed").Range("A" & Rows.Count).End(3)(2)
    .AutoFilter.Range.Offset(1).EntireRow.Delete
    .ShowAllData
  End With
  Application.ScreenUpdating = True
End Sub

VBA Code:
Sub MoveRowsToWorklist_v1()
  Application.ScreenUpdating = False
  With Sheets("Completed")
    .Range("A2", .Range("C" & Rows.Count).End(3)).AutoFilter 3, "<>Completed"
    .AutoFilter.Range.Offset(1).EntireRow.Copy Sheets("Worklist").Range("A" & Rows.Count).End(3)(2)
    .AutoFilter.Range.Offset(1).EntireRow.Delete
    .ShowAllData
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Thank you for your help DanteAmor

I have tested your approach, but it still fails on
.Range("A2", .Range("C" & Rows.Count).End(3)).AutoFilter 3, "<>Completed"
 
Upvote 0
I have tested your approach, but it still fails on
.Range("A2", .Range("C" & Rows.Count).End(3)).AutoFilter 3, "<>Completed"

Does it send you any errors? What does the error say?
You can put an image of your "Completed" sheet, there are supposed to be values in column "C" other than "Completed".
 
Upvote 0
Hi
Visual Basic gives me a Run-time error 1004: The method AutoFilter for the class Range dit not succed.
When I choose debug the above mentioned code line is highlighted.
I have attached a screenshot. It's in danish, but I have translated the names in my code (Fuldført=completed)

It works fine from Worklist->Completed but fails when going from Completed->Worklist

For me it seems to be the use of "<>" that causes the problem, but I don't know how else to exclude "Completed"
 

Attachments

  • Skærmbillede 2024-09-19 094932.png
    Skærmbillede 2024-09-19 094932.png
    18.7 KB · Views: 6
Upvote 0
I have no problems with "<>".
Maybe you have problems on that sheet. Rename that sheet.
Create a new sheet and name it "Completed", write some test data. In column C write data other than "Completed", try the macro again.
 
Upvote 0
That did the trick. I will recreate my "completed" sheet.
To find out if it was my sheet all along, I also tested my old code on the new sheet, but it still came up with the same error, so I will be using your code.

Thank you for your help DanteAmor. I will mark your first responce as solved.
 
Upvote 1

Forum statistics

Threads
1,224,798
Messages
6,181,038
Members
453,013
Latest member
Shubashish_Nandy

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