PDA

View Full Version : Cut And Paste Selected Rows



maninjapan
10-31-2008, 01:27 AM
Hey Guys Back again with another request for help. Trying to make a simple Macro for this. The idea is to paste trader AAAs results from the main sheet to their individual record sheet

If Column B = AAA Then Copy to Sheet5

Bob Phillips
10-31-2008, 03:50 AM
Dim LastRow As Long
Dim NextRow As Long
Dim i As Long

With Worksheets("Main")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow

If Worksheets(.Cells(i, "B").Value).Range("A1").Value = "" Then

NextRow = 1
Else

NextRow = Worksheets(.Cells(i, "B").Value).Cells(.Rows.Count, "A").End(xlUp).Row + 1
End If
.Rows(i).Copy Worksheets(.Cells(i, "B").Value).Cells(NextRow, "A")
Next i
End With

maninjapan
10-31-2008, 05:08 AM
xld, Im trying to understand the code you have put up. I tried it but it didnt seem to do anything. I think this should give a better example of wht I am trying to do. Copy each of the traders rows to their respective individual sheets.

GTO
11-01-2008, 12:24 AM
Not sure if it's as "pretty" (efficient) as it could be, but a small tweak to XLD's to handle empty first row or two of "Main" sheet ("Sheet1" in your wb example).

Hope this helps,

Mark

Sub CopyTo_()
Dim FirstRow As Long
Dim LastRow As Long
Dim NextRow As Long
Dim i As Long

With Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

For i = 1 To LastRow
'// Find the first row that has a value in col 'B' //
If Not .Cells(i, 2).Value = "" Then
FirstRow = i
Exit For
End If
Next

For i = FirstRow To LastRow
If Worksheets(.Cells(i, "B").Value).Range("A1").Value = "" Then

NextRow = 1
Else

NextRow = Worksheets(.Cells(i, "B").Value).Cells(.Rows.Count, "A") _
.End(xlUp).Row + 1
End If
.Rows(i).Copy Worksheets(.Cells(i, "B").Value).Cells(NextRow, "A")
Next i
End With
End Sub

maninjapan
11-03-2008, 02:10 AM
Thanks workd fine. I have aproblem using this just as it is now though. All the Data will be kept in the main sheet and added to daily. I need it to only look at the most recent days data to copy across.
How could I add If Column A = most recent Date. I would also like to add a heading row in row 1, How can I have it ignore The First Row when looking at Column B?

Bob Phillips
11-03-2008, 02:29 AM
Public Sub ProcessData()
Dim LastRow As Long
Dim NextRow As Long
Dim i As Long

With Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

For i = 1 To LastRow

If .Cells(i, "A").Value = Application.Max(.Columns(1)) Then

If Worksheets(.Cells(i, "B").Value).Range("A1").Value = "" Then

NextRow = 1
Else

NextRow = Worksheets(.Cells(i, "B").Value).Cells(.Rows.Count, "A").End(xlUp).Row + 1
End If

.Rows(i).Copy Worksheets(.Cells(i, "B").Value).Cells(NextRow, "A")
End If
Next i
End With
End Sub