IF-Then statement to copy rows
Solved/Closed
froggy6703
Posts
16
Registration date
Tuesday 23 March 2010
Status
Member
Last seen
29 March 2013
-
28 Dec 2010 à 13:59
froggy6703 Posts 16 Registration date Tuesday 23 March 2010 Status Member Last seen 29 March 2013 - 4 Jan 2011 à 10:54
froggy6703 Posts 16 Registration date Tuesday 23 March 2010 Status Member Last seen 29 March 2013 - 4 Jan 2011 à 10:54
Related:
- Excel copy row to another sheet if cell contains certain text
- How to copy data from one excel sheet to another - Guide
- Copying data from one Excel sheet to another. ✓ - Excel Forum
- Excel add 1 if cell contains text ✓ - Excel Forum
- Excel hyperlink to another sheet - Guide
- Excel move data from one sheet to another - Guide
1 response
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
30 Dec 2010 à 09:44
30 Dec 2010 à 09:44
Hi Froggy,
Try this code:
Can you work with this?
Ask away if you get stuck?
Best regards,
Trowa
Try this code:
Sub test()
Set MR = Sheets("Input sheet").Range("B2:B5")
For Each cell In MR
If cell.Value = 2010 And cell.Offset(0, 1).Value = "Minor" Then
cell.EntireRow.Copy
Sheets("Minor Child 2010").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
If cell.Value = 2011 And cell.Offset(0, 1).Value = "Minor" Then
cell.EntireRow.Copy
Sheets("Minor Child 2011").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
If cell.Value = 2010 And cell.Offset(0, 1).Value = "Adult" Then
cell.EntireRow.Copy
Sheets("Adult Child 2010").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
If cell.Value = 2011 And cell.Offset(0, 1).Value = "Adult" Then
cell.EntireRow.Copy
Sheets("Adult Child 2011").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
Application.CutCopyMode = False
Next
End Sub
Can you work with this?
Ask away if you get stuck?
Best regards,
Trowa
30 Dec 2010 à 12:21
property info1 info2 info3
A 1 2 3
B 2 3 4
C 3 4 1
D 4 1 2
In this example, if the row is for Property A, it will copy the entire row from Sheet 'Master' to Sheet 'A'
I've never done this before, so I don't even know how and where to execute the code. What I came up with:
Sub Data_move()
Set MR = Sheets("Main").Range("a2:a8")
For Each cell In MR
If cell.Offset(0, 1).Value = "A" Then
cell.EntireRow.Copy
Sheets("A").Range("a" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
If cell.Offset(0, 1).Value = "B" Then
cell.EntireRow.Copy
Sheets("B").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
If cell.Offset(0, 1).Value = "C" Then
cell.EntireRow.Copy
Sheets("C").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
If cell.Offset(0, 1).Value = "D" Then
cell.EntireRow.Copy
Sheets("D").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
Application.CutCopyMode = False
Next
End Sub
What am I doing wrong?
30 Dec 2010 à 12:30
4 Jan 2011 à 10:04
Running the code automatically doesn't solve the duplicate issue.
I can think of two ways to solve this issue.
1. Add a code line to remove the data from the destination sheets first:
Sub Data_move() Sheets("A").Cells.ClearContents Sheets("B").Cells.ClearContents Sheets("C").Cells.ClearContents Sheets("D").Cells.ClearContents Set MR = Sheets("Main").Range("a2:a8")Downside is that when you have a lot of data, the code will take a while to run. Adding more data and the code takes even longer.
2. Actively select the range you want to move:
Downside here is that you have to pay attention to the range you select in order to prevent mistakes.
Hopes this helps.
Best regards,
Trowa
4 Jan 2011 à 10:54
Froggy