Forum Discussion
cak980
May 05, 2022Copper Contributor
Using VBA to move a range of data to a different sheet when complete (need the for dummies version)
I'm trying to setup a macro so that when a task is completed (indicated with X), it automatically gets moved to a separate tab and deleted from original location. I was able to accomplish this but i...
cak980
May 05, 2022Copper Contributor
Sub move_rows_to_another_sheet()
For Each myCell In Selection.Columns(4).Cells
If myCell.Value = "Closed" Then
myCell.EntireRow.Copy Worksheets("Sheet2").Range("A" & Rows.Count).End(3)(2)
myCell.EntireRow.Delete
End If
Next
End Sub
For Each myCell In Selection.Columns(4).Cells
If myCell.Value = "Closed" Then
myCell.EntireRow.Copy Worksheets("Sheet2").Range("A" & Rows.Count).End(3)(2)
myCell.EntireRow.Delete
End If
Next
End Sub
HansVogelaar
May 05, 2022MVP
Thanks!
Because you use EntireRow, the entire row gets moved.
Which column or columns in the row would you like to move?
- austin2134Aug 30, 2022Copper ContributorI found the same code online and i'm changing it to fit my file.
I also only want to extract from certain columns "A","E", and "F". What would you change in this row:
myCell.EntireRow.Copy Worksheets("Sheet2").Range("A" & Rows.Count).End(3)(2)- HansVogelaarAug 30, 2022MVP
Do you want to copy them to columns A, E and F, or for example to columns A, B and C?
- austin2134Aug 30, 2022Copper ContributorThat was going to be my next question.
From the original sheet, I only want 3/8 columns(A, E, and F). Once I figure that out, I want to place them into certain cells such as A, B, and C onto the next sheet.
- cak980May 06, 2022Copper Contributor
HansVogelaar A-E
- HansVogelaarMay 06, 2022MVP
Try this:
Sub move_rows_to_another_sheet() Dim myCell As Range Dim w As Worksheet Dim s As Long Dim m As Long Dim t As Long Dim rng As Range Set w = Worksheets("Sheet2") t = w.Range("A" & w.Rows.Count).End(xlUp).Row Application.ScreenUpdating = False m = Selection.Rows.Count For s = m To 1 Step -1 If Selection.Range("D" & s).Value = "Closed" Then t = t + 1 Set rng = Range("A" & Selection.Range("D" & s).Row).Resize(ColumnSize:=4) rng.Copy Destination:=w.Range("A" & t) rng.Delete Shift:=xlShiftUp End If Next s Application.ScreenUpdating = True End Sub
- cak980May 06, 2022Copper ContributorI must be doing something wrong, it's not working. I made some edits - sheet 2 is actually "Completed" and "Closed" is actually "X" and is column E